240-202 Comp Eng Lab II: Intro. to Prolog 1 Computer Engineering Lab II 240-202, Semester 2,...

58
240-202 Comp Eng Lab II: Intro. to Prolog 1 Computer Engineering Lab II 240-202, Semester 2, 2005-20 06 Who I am: Andrew Davison CoE, Info. Eng. Research Lab (Rm 101) [email protected] Introduction to Prolo g Please ask questions

Transcript of 240-202 Comp Eng Lab II: Intro. to Prolog 1 Computer Engineering Lab II 240-202, Semester 2,...

Page 1: 240-202 Comp Eng Lab II: Intro. to Prolog 1 Computer Engineering Lab II 240-202, Semester 2, 2005-2006 Who I am: Andrew Davison CoE, Info. Eng. Research.

240-202 Comp Eng Lab II: Intro. to Prolog

1

Computer Engineering Lab II- 240202 2 2, Semester ,-0052006

Who I am: Andrew Davison

CoE, Info. Eng. Research Lab (Rm 1 0 1 )[email protected]

Introduction to Prolog Please ask

questions

Page 2: 240-202 Comp Eng Lab II: Intro. to Prolog 1 Computer Engineering Lab II 240-202, Semester 2, 2005-2006 Who I am: Andrew Davison CoE, Info. Eng. Research.

240-202 Comp Eng Lab II: Intro. to Prolog

2

Outline

1. Language Elements2. Executing Prolog3. Rules4. A Database in Prolog5. Recursive Rules6. Operators 7. Lists

continued

Page 3: 240-202 Comp Eng Lab II: Intro. to Prolog 1 Computer Engineering Lab II 240-202, Semester 2, 2005-2006 Who I am: Andrew Davison CoE, Info. Eng. Research.

240-202 Comp Eng Lab II: Intro. to Prolog

3

PPPPPPPPP8. / 3 P PPPP PPPP PPPPPPPPPP9. PPPPPPPPP PPPP10. 11 1. The not/ Predicate 12. Using Strawberry Prolog 13. More Information

Page 4: 240-202 Comp Eng Lab II: Intro. to Prolog 1 Computer Engineering Lab II 240-202, Semester 2, 2005-2006 Who I am: Andrew Davison CoE, Info. Eng. Research.

240-202 Comp Eng Lab II: Intro. to Prolog

4

Acknowledgements

The material for this presentation ca me from three main sources:

“A First Look at Prolog”Adam Webberhttp://www.webber-labs.com/mpl/lectures/19.ppt

“Prolog in 90 minutes”Ulf Nilsson, Linköping University

me!

Page 5: 240-202 Comp Eng Lab II: Intro. to Prolog 1 Computer Engineering Lab II 240-202, Semester 2, 2005-2006 Who I am: Andrew Davison CoE, Info. Eng. Research.

240-202 Comp Eng Lab II: Intro. to Prolog

5

1. Language Elements

TermsFacts and Rules

they use terms as data structures

Predicates made up of facts and rules

The (Logic) Program made up of predicates

Page 6: 240-202 Comp Eng Lab II: Intro. to Prolog 1 Computer Engineering Lab II 240-202, Semester 2, 2005-2006 Who I am: Andrew Davison CoE, Info. Eng. Research.

240-202 Comp Eng Lab II: Intro. to Prolog

6

Terms

Terms are data.Constants

adam paris 5 3.14 [] ´Adam’ ...Variables

X Y List _12 _ ...Compound terms (something like

structs) plus(2,3) foo(2,bar(a,'Jim')) ... 2+3 // infix notation

Page 7: 240-202 Comp Eng Lab II: Intro. to Prolog 1 Computer Engineering Lab II 240-202, Semester 2, 2005-2006 Who I am: Andrew Davison CoE, Info. Eng. Research.

240-202 Comp Eng Lab II: Intro. to Prolog

7

Facts

A fact has the form p(t1,...,tn). p is the name of the fact t1, …, tn are term arguments of the fact

Examples:edge(a, X).

parent(adam, bill).

note the '.'s

Page 8: 240-202 Comp Eng Lab II: Intro. to Prolog 1 Computer Engineering Lab II 240-202, Semester 2, 2005-2006 Who I am: Andrew Davison CoE, Info. Eng. Research.

240-202 Comp Eng Lab II: Intro. to Prolog

8

Example

Six facts about families: kim is the parent of holly

Defines a predicate parent of arity 2 parent/2 for short

parent(kim,holly).parent(margaret,kim).parent(margaret,kent).parent(esther,margaret).parent(herbert,margaret).parent(herbert,jean).

note the '.'s

Page 9: 240-202 Comp Eng Lab II: Intro. to Prolog 1 Computer Engineering Lab II 240-202, Semester 2, 2005-2006 Who I am: Andrew Davison CoE, Info. Eng. Research.

240-202 Comp Eng Lab II: Intro. to Prolog

9

Facts are not Terms

A fact : parent(kim, holly).is part of the program.

A term: foo(1, 4)is a piece of data used inside a fact (or rule).

Page 10: 240-202 Comp Eng Lab II: Intro. to Prolog 1 Computer Engineering Lab II 240-202, Semester 2, 2005-2006 Who I am: Andrew Davison CoE, Info. Eng. Research.

240-202 Comp Eng Lab II: Intro. to Prolog

10

Facts using Term Data

A staff/2 predicate: 101staff( name(andrew), room( ) ).

403staff( name(mondri), room( ) ). 301staff( name(chatchai), room( ) ).

Each staff/2 fact uses two terms.

Page 11: 240-202 Comp Eng Lab II: Intro. to Prolog 1 Computer Engineering Lab II 240-202, Semester 2, 2005-2006 Who I am: Andrew Davison CoE, Info. Eng. Research.

240-202 Comp Eng Lab II: Intro. to Prolog

11

Logic Programs

A logic program is a set of predicates: a predicate is made of facts and rules

The program is used to answer user queries.

Prolog is a logic programming language.

Page 12: 240-202 Comp Eng Lab II: Intro. to Prolog 1 Computer Engineering Lab II 240-202, Semester 2, 2005-2006 Who I am: Andrew Davison CoE, Info. Eng. Research.

240-202 Comp Eng Lab II: Intro. to Prolog

12

2. Executing Prolog more details in section 1 2

Page 13: 240-202 Comp Eng Lab II: Intro. to Prolog 1 Computer Engineering Lab II 240-202, Semester 2, 2005-2006 Who I am: Andrew Davison CoE, Info. Eng. Research.

240-202 Comp Eng Lab II: Intro. to Prolog

13

Use a query to execute Prolog code.

In SB Prolog: execute query, print results with

write()s use F5 (or Run|Run) to execute the

query the output appears in the Output

window.

Page 14: 240-202 Comp Eng Lab II: Intro. to Prolog 1 Computer Engineering Lab II 240-202, Semester 2, 2005-2006 Who I am: Andrew Davison CoE, Info. Eng. Research.

240-202 Comp Eng Lab II: Intro. to Prolog

14

Simple Queries

The queries: ?- parent(margaret, kent). Yes // printed in the Output window

?- parent(fred, pebbles). No

Page 15: 240-202 Comp Eng Lab II: Intro. to Prolog 1 Computer Engineering Lab II 240-202, Semester 2, 2005-2006 Who I am: Andrew Davison CoE, Info. Eng. Research.

240-202 Comp Eng Lab II: Intro. to Prolog

15

Queries With Variables

Query: ?- parent(P, jean), write(P), write("\n") herbert

Yes

?- parent(P, esther), write(P), write("\n") No

In SB Prolog, the variable bindings must be printed by the query.

Page 16: 240-202 Comp Eng Lab II: Intro. to Prolog 1 Computer Engineering Lab II 240-202, Semester 2, 2005-2006 Who I am: Andrew Davison CoE, Info. Eng. Research.

240-202 Comp Eng Lab II: Intro. to Prolog

16

Flexibility

Variables can appear anywhere in a query: ?- parent(Parent,jean). ?- parent(esther,Child). ?- parent(Parent,Child). ?- parent(Person,Person).

Page 17: 240-202 Comp Eng Lab II: Intro. to Prolog 1 Computer Engineering Lab II 240-202, Semester 2, 2005-2006 Who I am: Andrew Davison CoE, Info. Eng. Research.

240-202 Comp Eng Lab II: Intro. to Prolog

17

Conjunctions

A conjunction is a series of queries.The Prolog system tries to prove

them all by making variable bindings e.g. bind X to kim

?- parent(margaret,X), parent(X,holly), write(X), write("\n").

kim Yes

PPPP PPP PPP

Page 18: 240-202 Comp Eng Lab II: Intro. to Prolog 1 Computer Engineering Lab II 240-202, Semester 2, 2005-2006 Who I am: Andrew Davison CoE, Info. Eng. Research.

240-202 Comp Eng Lab II: Intro. to Prolog

18

Multiple Solutions

PPP PPPP PPPPP 8'F ' to make

PP PPPPPP PPPP PPP PPPPPPP answer

Page 19: 240-202 Comp Eng Lab II: Intro. to Prolog 1 Computer Engineering Lab II 240-202, Semester 2, 2005-2006 Who I am: Andrew Davison CoE, Info. Eng. Research.

240-202 Comp Eng Lab II: Intro. to Prolog

19

3. Rules

To prove the head, prove the conditions.

To prove greatgrandparent(GGP,GGC), find some GP and P for which you can prove parent(GGP,GP), then parent(GP,P), and finally parent(P,GGC).

greatgrandparent(GGP,GGC) :- parent(GGP,GP), parent(GP,P), parent(P,GGC). conditions

head

Page 20: 240-202 Comp Eng Lab II: Intro. to Prolog 1 Computer Engineering Lab II 240-202, Semester 2, 2005-2006 Who I am: Andrew Davison CoE, Info. Eng. Research.

240-202 Comp Eng Lab II: Intro. to Prolog

20

A Program With a Rule

parent/2 and greatgrandparent/2 predicates.

parent(kim,holly).parent(margaret,kim).parent(margaret,kent).parent(esther,margaret).parent(herbert,margaret).parent(herbert,jean).

greatgrandparent(GGP,GGC) :- parent(GGP,GP), parent(GP,P), parent(P,GGC).

Page 21: 240-202 Comp Eng Lab II: Intro. to Prolog 1 Computer Engineering Lab II 240-202, Semester 2, 2005-2006 Who I am: Andrew Davison CoE, Info. Eng. Research.

240-202 Comp Eng Lab II: Intro. to Prolog

21

2parent/ as a Graph

est her herbert

P PPPPPPP

PPP kent

holly

PPPPPPPPPP

Page 22: 240-202 Comp Eng Lab II: Intro. to Prolog 1 Computer Engineering Lab II 240-202, Semester 2, 2005-2006 Who I am: Andrew Davison CoE, Info. Eng. Research.

240-202 Comp Eng Lab II: Intro. to Prolog

22

Example

This shows the initial query and final result.

There are intermediate goals inside the system:

?- greatgrandparen(esther,GreatGrandchild), write(GreatGrandchild), write("\n").

hollyYes

continued

Page 23: 240-202 Comp Eng Lab II: Intro. to Prolog 1 Computer Engineering Lab II 240-202, Semester 2, 2005-2006 Who I am: Andrew Davison CoE, Info. Eng. Research.

240-202 Comp Eng Lab II: Intro. to Prolog

23

greatgrandparent(esther,GreatGrandchild)

1. parent(kim,holly).2. parent(margaret,kim).3. parent(margaret,kent).4. parent(esther,margaret).5. parent(herbert,margaret).6. parent(herbert,jean).7. greatgrandparent(GGP,GGC) :- parent(GGP,GP), parent(GP,P), parent(P,GGC).

parent(esther,GP), parent(GP,P), parent(P,GreatGrandchild)

parent(margaret,P), parent(P,GreatGrandchild)

parent(kim,GreatGrandchild)

Clause 7, binding GGP to esther and GGC to GreatGrandChild

Clause 4, binding GP to margaret

Clause 2, binding P to kim

Clause 1, binding GreatGrandchild to holly

Page 24: 240-202 Comp Eng Lab II: Intro. to Prolog 1 Computer Engineering Lab II 240-202, Semester 2, 2005-2006 Who I am: Andrew Davison CoE, Info. Eng. Research.

240-202 Comp Eng Lab II: Intro. to Prolog

24

Rules Using Other Rules

Note that both rules use a variable P.The scope of the definition of a variable

is the fact/rule that contains it.

greatgrandparent(GGP,GGC) :- grandparent(GGP,P), parent(P,GGC).

grandparent(GP,GC) :- parent(GP,P), parent(P,GC).

Page 25: 240-202 Comp Eng Lab II: Intro. to Prolog 1 Computer Engineering Lab II 240-202, Semester 2, 2005-2006 Who I am: Andrew Davison CoE, Info. Eng. Research.

240-202 Comp Eng Lab II: Intro. to Prolog

25

4. A Database in Prolog

lecturer(Lecturer,Course) :- course(Course,_,Lecturer,_).

duration(Course,Length) :- course(Course,time(_,S,F),_,_), Length is F-S.

teaches(Lect,Day) :- course(_, time(Day,_,_), Lect, _). occupied(Room,Day,Time) :- course(_,time(Day,S,F),_,Room), S =< Time, Time =< F.

% Databasecourse(logic, time(monday, 8, 10), dave, a12).course(java, time(tuesday, 9, 11), ad, r204).

:

Page 26: 240-202 Comp Eng Lab II: Intro. to Prolog 1 Computer Engineering Lab II 240-202, Semester 2, 2005-2006 Who I am: Andrew Davison CoE, Info. Eng. Research.

240-202 Comp Eng Lab II: Intro. to Prolog

26

Some Queries

P- PPPPPPPPPPP PPPPPPP who teaches logic?

- ? duration(C, 2). what course is 2 hours long?

- ? occupied(r204, wednesday, Time). when is r204 occupied on Wednesday?

continued

Page 27: 240-202 Comp Eng Lab II: Intro. to Prolog 1 Computer Engineering Lab II 240-202, Semester 2, 2005-2006 Who I am: Andrew Davison CoE, Info. Eng. Research.

240-202 Comp Eng Lab II: Intro. to Prolog

27

SB Prolog Queries: ?- lecturer(L, logic), write(L), write("\n").daveyes

?- duration(C,2), write(C), write("\n").logicjava // I typed F8no // I typed F8

?- occupied(r204, wednesday, Time).no

Page 28: 240-202 Comp Eng Lab II: Intro. to Prolog 1 Computer Engineering Lab II 240-202, Semester 2, 2005-2006 Who I am: Andrew Davison CoE, Info. Eng. Research.

240-202 Comp Eng Lab II: Intro. to Prolog

28

5. Recursive Rules

X is an ancestor of Y if: Base case: X is a parent of Y Recursive case: there is some Z such that Z is

a parent of Y, and X is an ancestor of ZProlog tries rules in the order you give

them, so put base-case rules and facts first.

ancestor(X,Y) :- parent(X,Y).ancestor(X,Y) :- parent(Z,Y), ancestor(X,Z).

Page 29: 240-202 Comp Eng Lab II: Intro. to Prolog 1 Computer Engineering Lab II 240-202, Semester 2, 2005-2006 Who I am: Andrew Davison CoE, Info. Eng. Research.

240-202 Comp Eng Lab II: Intro. to Prolog

29

parent/2 again

parent(kim,holly).parent(margaret,kim).parent(margaret,kent).parent(esther,margaret).parent(herbert,margaret).parent(herbert,jean).

est her herbert

P PPPPPPP

PPP kent

holly

PPPP

Page 30: 240-202 Comp Eng Lab II: Intro. to Prolog 1 Computer Engineering Lab II 240-202, Semester 2, 2005-2006 Who I am: Andrew Davison CoE, Info. Eng. Research.

240-202 Comp Eng Lab II: Intro. to Prolog

30

?- ancestor(kim,holly).Yes

?- ancestor(A,holly), write(A), write("\n").kimmargaret // I typed F8esther // typed F8herbert // typed F8No // typed F8

ancestor/2 Queries

Page 31: 240-202 Comp Eng Lab II: Intro. to Prolog 1 Computer Engineering Lab II 240-202, Semester 2, 2005-2006 Who I am: Andrew Davison CoE, Info. Eng. Research.

240-202 Comp Eng Lab II: Intro. to Prolog

31

Path Searching

path(Node,Node).path(Node1,Node3) :-

edge(Node1,Node2),path(Node2,Node3).

edge(a,b).edge(a,c).edge(b,d).edge(c,d).edge(d,e).edge(f,g).

a

b

c

d

e

f

g

Page 32: 240-202 Comp Eng Lab II: Intro. to Prolog 1 Computer Engineering Lab II 240-202, Semester 2, 2005-2006 Who I am: Andrew Davison CoE, Info. Eng. Research.

240-202 Comp Eng Lab II: Intro. to Prolog

32

Some Queries

- ? (, ). is there a path from a to e?

- ? path(a, Y). what node can be reached from a?

- ? path(X, d). P PPP PPPP PPP P PPPP PP PP

continued

Page 33: 240-202 Comp Eng Lab II: Intro. to Prolog 1 Computer Engineering Lab II 240-202, Semester 2, 2005-2006 Who I am: Andrew Davison CoE, Info. Eng. Research.

240-202 Comp Eng Lab II: Intro. to Prolog

33

Note the multiple answers, by using F

8

SB Prolog queries: ? - path(a, e).

yes

?- path(a,Y), write(Y), write("\n").abdecdeno

?- path(X,d), write(X), write("\n").daabcno

Page 34: 240-202 Comp Eng Lab II: Intro. to Prolog 1 Computer Engineering Lab II 240-202, Semester 2, 2005-2006 Who I am: Andrew Davison CoE, Info. Eng. Research.

240-202 Comp Eng Lab II: Intro. to Prolog

34

6. Operators

Prolog systems have many built-in operators. e.g. =/2 is/2 and arithmetic

Page 35: 240-202 Comp Eng Lab II: Intro. to Prolog 1 Computer Engineering Lab II 240-202, Semester 2, 2005-2006 Who I am: Andrew Davison CoE, Info. Eng. Research.

240-202 Comp Eng Lab II: Intro. to Prolog

35

The =/2 Predicate

The goal =(X,Y) succeeds if X and Y can be unified "unify" means create variable bindings

Usually written as X = Y

?- name(adam,seth) = name(adam,X), write(X), write("\n").seth Yes

Page 36: 240-202 Comp Eng Lab II: Intro. to Prolog 1 Computer Engineering Lab II 240-202, Semester 2, 2005-2006 Who I am: Andrew Davison CoE, Info. Eng. Research.

240-202 Comp Eng Lab II: Intro. to Prolog

36

Arithmetic Operators

Terms +, -, * and / are operators, with the usual precedence and associativity

?- X = +(1,*(2,3)).X = 1+2*3

?- X = 1+2*3.X = 1+2*3

No evaluation yet.

Page 37: 240-202 Comp Eng Lab II: Intro. to Prolog 1 Computer Engineering Lab II 240-202, Semester 2, 2005-2006 Who I am: Andrew Davison CoE, Info. Eng. Research.

240-202 Comp Eng Lab II: Intro. to Prolog

37

Evaluating Arithmetic

The general format is:Variable is operation_term

?- X is 1+2*3, write(X), write("\n").7

yes

Page 38: 240-202 Comp Eng Lab II: Intro. to Prolog 1 Computer Engineering Lab II 240-202, Semester 2, 2005-2006 Who I am: Andrew Davison CoE, Info. Eng. Research.

240-202 Comp Eng Lab II: Intro. to Prolog

38

7. Lists

The atom [] represents the empty list.

List notation Actual term

[] []

[1] .(1,[])

[1,2,3] .(1,.(2,.(3,[])))

[1,name(X,Y)] .(1,.(name(X,Y),[]))

Page 39: 240-202 Comp Eng Lab II: Intro. to Prolog 1 Computer Engineering Lab II 240-202, Semester 2, 2005-2006 Who I am: Andrew Davison CoE, Info. Eng. Research.

240-202 Comp Eng Lab II: Intro. to Prolog

39

Examples

?- X = .(1,.(2,.(3,[]))).X = [1, 2, 3]

?- [X, Y, Z] = [1, 2, 3].X = 1Y = 2Z = 3

Page 40: 240-202 Comp Eng Lab II: Intro. to Prolog 1 Computer Engineering Lab II 240-202, Semester 2, 2005-2006 Who I am: Andrew Davison CoE, Info. Eng. Research.

240-202 Comp Eng Lab II: Intro. to Prolog

40

List Notation With Tail

[1,2|X] unifies with a list that starts with 1,2 and binds X to the tail.

?- [1,2|X] = [1,2,3,4,5].X = [3, 4, 5]

Page 41: 240-202 Comp Eng Lab II: Intro. to Prolog 1 Computer Engineering Lab II 240-202, Semester 2, 2005-2006 Who I am: Andrew Davison CoE, Info. Eng. Research.

240-202 Comp Eng Lab II: Intro. to Prolog

41

8. The append/3 Predicate

append(X,Y,Z) succeeds when Z is the same as the list Y appended onto the end of the list X.

?- append([1,2],[3,4],Z), write(Z), write("\n").[1, 2, 3, 4] Yes

append([], B, B).append([Head|TailA], B, [Head|TailC]) :- append(TailA, B, TailC).

Page 42: 240-202 Comp Eng Lab II: Intro. to Prolog 1 Computer Engineering Lab II 240-202, Semester 2, 2005-2006 Who I am: Andrew Davison CoE, Info. Eng. Research.

240-202 Comp Eng Lab II: Intro. to Prolog

42

Other Uses of append/3

append/3 can be called with variables in any of its argument positions.

?- append(X,[3,4],[1,2,3,4]), write(X), write("\n").[1, 2] Yes

Page 43: 240-202 Comp Eng Lab II: Intro. to Prolog 1 Computer Engineering Lab II 240-202, Semester 2, 2005-2006 Who I am: Andrew Davison CoE, Info. Eng. Research.

240-202 Comp Eng Lab II: Intro. to Prolog

43

Multiple Answers

?- append(X,Y,[1,2,3]).X = []Y = [1, 2, 3]

X = [1]Y = [2, 3]

X = [1, 2]Y = [3]

X = [1, 2, 3]Y = []

No

By using F8

Page 44: 240-202 Comp Eng Lab II: Intro. to Prolog 1 Computer Engineering Lab II 240-202, Semester 2, 2005-2006 Who I am: Andrew Davison CoE, Info. Eng. Research.

240-202 Comp Eng Lab II: Intro. to Prolog

44

In SB Prolog: writeBind(Str, Val) :-

write(Str), write(" = "), write(Val), write("\n").

?- append(X, Y,[1,2,3]), writeBind("X", X), writeBind("Y", Y).

Page 45: 240-202 Comp Eng Lab II: Intro. to Prolog 1 Computer Engineering Lab II 240-202, Semester 2, 2005-2006 Who I am: Andrew Davison CoE, Info. Eng. Research.

240-202 Comp Eng Lab II: Intro. to Prolog

45

9. Other List Predicates

Flexible, like append/3 queries can contain variables anywhere.

Predicate Description

member(X,Y) Succeeds if X is an element in the list Y.

length(X,Y) Succeeds if the list X is length Y.

Page 46: 240-202 Comp Eng Lab II: Intro. to Prolog 1 Computer Engineering Lab II 240-202, Semester 2, 2005-2006 Who I am: Andrew Davison CoE, Info. Eng. Research.

240-202 Comp Eng Lab II: Intro. to Prolog

46

member/2P PP PPPPPP PPPPPPP

-member(X, [Y|Rest]) : member(X, Rest).

member(X,L) succeeds if X is an element in the list L

?- member(1,[1,2,3]).yes

?- member(a,[a,n,d,y]).yes

?- member(X,[j,i,m]), write(X), write("\n").ji // F8m // F8no // F8

Page 47: 240-202 Comp Eng Lab II: Intro. to Prolog 1 Computer Engineering Lab II 240-202, Semester 2, 2005-2006 Who I am: Andrew Davison CoE, Info. Eng. Research.

240-202 Comp Eng Lab II: Intro. to Prolog

47

length/2

length(X,Y) succeeds if Y is the length of the list X.

0length([], ).PPPPPPPPPPPPPPPP PPPP P- length(Rest, LenRest), 1Len is LenRest + .?- length([a,b,c,d], L), write(L), write("\n").

4

?- length([1,2,3], 4).no

Page 48: 240-202 Comp Eng Lab II: Intro. to Prolog 1 Computer Engineering Lab II 240-202, Semester 2, 2005-2006 Who I am: Andrew Davison CoE, Info. Eng. Research.

240-202 Comp Eng Lab II: Intro. to Prolog

48

10. Insertion Sort

% isort(A,B): B is a sorted version of Aisort([X|Xs],Ys) :- isort(Xs,Zs), insert(X,Zs,Ys).isort([ ],[ ]).

% insert(A,B,C)% if B is a sorted list, then C is sorted% and contains all elements in B plus Ainsert(X,[ ],[X]).insert(X,[Y|Ys],[Y|Zs]) :- X > Y, insert(X,Ys,Zs).insert(X,[Y|Ys],[X,Y|Ys]) :- X =< Y.

- 4 3 1? isort([ , , , 5], S)

write(S), write("\n"PP

S = [1, 3, 4, 5]PPP

Page 49: 240-202 Comp Eng Lab II: Intro. to Prolog 1 Computer Engineering Lab II 240-202, Semester 2, 2005-2006 Who I am: Andrew Davison CoE, Info. Eng. Research.

240-202 Comp Eng Lab II: Intro. to Prolog

49

11. The not/1 Predicate

Built-in not(X) succeeds when X fails.Only use not/1 when its goal contains no

variables. (Use not/1 only as a test.)

?- not( member(4,[1,2,3]) ).Yes

?- not( member(1,[1,2,3]) ).No

Page 50: 240-202 Comp Eng Lab II: Intro. to Prolog 1 Computer Engineering Lab II 240-202, Semester 2, 2005-2006 Who I am: Andrew Davison CoE, Info. Eng. Research.

240-202 Comp Eng Lab II: Intro. to Prolog

50

Example

sibling(X,Y) :- not(X=Y), parent(P,X), parent(P,Y).

?- sibling(kim,kent).Yes

?- sibling(kim,kim).No

Page 51: 240-202 Comp Eng Lab II: Intro. to Prolog 1 Computer Engineering Lab II 240-202, Semester 2, 2005-2006 Who I am: Andrew Davison CoE, Info. Eng. Research.

240-202 Comp Eng Lab II: Intro. to Prolog

51

12. Using Strawberry Prolog

Download the installation program for the SB Prolog system from:

http://fivedots.coe.psu.ac.th/Software.coe/LAB/Prolog/

The filename:StrawberryProlog2_5_for_98.exe

Read the readme.txt file in the same directory.

Page 52: 240-202 Comp Eng Lab II: Intro. to Prolog 1 Computer Engineering Lab II 240-202, Semester 2, 2005-2006 Who I am: Andrew Davison CoE, Info. Eng. Research.

240-202 Comp Eng Lab II: Intro. to Prolog

52

Start SB Prolog from C:\Program Files\Strawberry Prolog\Prolog.exe:

Page 53: 240-202 Comp Eng Lab II: Intro. to Prolog 1 Computer Engineering Lab II 240-202, Semester 2, 2005-2006 Who I am: Andrew Davison CoE, Info. Eng. Research.

240-202 Comp Eng Lab II: Intro. to Prolog

53

Create a Prolog Program

saved in c:/windows/desktop/parents.pro

PPPPP PPP PPPP PPPPPP

Page 54: 240-202 Comp Eng Lab II: Intro. to Prolog 1 Computer Engineering Lab II 240-202, Semester 2, 2005-2006 Who I am: Andrew Davison CoE, Info. Eng. Research.

240-202 Comp Eng Lab II: Intro. to Prolog

54

Alternatively, you can just type into a new Prolog file window, then save at t

he end. PPPPPPPPP PPP PPPPPP PPPPPPP PPPPP |,"" attheend, sel ect Fi l e|Save

Page 55: 240-202 Comp Eng Lab II: Intro. to Prolog 1 Computer Engineering Lab II 240-202, Semester 2, 2005-2006 Who I am: Andrew Davison CoE, Info. Eng. Research.

240-202 Comp Eng Lab II: Intro. to Prolog

55

Running parents.pro

Load the file using the menu item File|Open.

Type in a query into the parents.pro window: -? parent(X, Y), write(X), write( " and

"), write(Y). Select Run|Run from the menu Check the Output window

Page 56: 240-202 Comp Eng Lab II: Intro. to Prolog 1 Computer Engineering Lab II 240-202, Semester 2, 2005-2006 Who I am: Andrew Davison CoE, Info. Eng. Research.

240-202 Comp Eng Lab II: Intro. to Prolog

56

13. More Information

The Help menu in the SB Prolog syste m leads to:

a t ut or i al a Prolog language guide PPPPPPPP

continued

Page 57: 240-202 Comp Eng Lab II: Intro. to Prolog 1 Computer Engineering Lab II 240-202, Semester 2, 2005-2006 Who I am: Andrew Davison CoE, Info. Eng. Research.

240-202 Comp Eng Lab II: Intro. to Prolog

57

SB Prolog’s Web Site: http://www.dobrev.com/light.html

other versions

A complete Prolog book (in PDF format): http://www.ida.liu.se/~ulfni/lpp/

continued

Page 58: 240-202 Comp Eng Lab II: Intro. to Prolog 1 Computer Engineering Lab II 240-202, Semester 2, 2005-2006 Who I am: Andrew Davison CoE, Info. Eng. Research.

240-202 Comp Eng Lab II: Intro. to Prolog

58

Online Prolog tutorials: http://cs.wwc.edu/~cs_dept/KU/PR/

Prolog.html http://cbl.leeds.ac.uk/~tamsin/

prologtutorial/ links to another online Prolog book

http://www.compapp.dcu.ie/~jhayes/Logic/ http://www.csupomona.edu/~jrfisher/www/

prolog_tutorial/pt_framer.html