+ All Categories
Home > Documents > Prolog. Syllogisms “Prolog” is all about programming in logic. –Socrates is a man. –All men...

Prolog. Syllogisms “Prolog” is all about programming in logic. –Socrates is a man. –All men...

Date post: 04-Jan-2016
Category:
Upload: anastasia-stevenson
View: 215 times
Download: 3 times
Share this document with a friend
28
Prolog
Transcript
Page 1: Prolog. Syllogisms “Prolog” is all about programming in logic. –Socrates is a man. –All men are mortal. –Therefore, Socrates is mortal.

Prolog

Page 2: Prolog. Syllogisms “Prolog” is all about programming in logic. –Socrates is a man. –All men are mortal. –Therefore, Socrates is mortal.

Syllogisms

• “Prolog” is all about programming in logic.– Socrates is a man.– All men are mortal.– Therefore, Socrates is mortal.

Page 3: Prolog. Syllogisms “Prolog” is all about programming in logic. –Socrates is a man. –All men are mortal. –Therefore, Socrates is mortal.

Facts, rules, and queries

• Fact: Socrates is a man.

• man(socrates).• Rule: All men are mortal.

• mortal(X) :- man(X).• Query: Is Socrates mortal?

• mortal(socrates).

Page 4: Prolog. Syllogisms “Prolog” is all about programming in logic. –Socrates is a man. –All men are mortal. –Therefore, Socrates is mortal.

Running Prolog I

• Create your "database" (program) in any editor

• Save it as text only, with a .pl extension

• Here's the complete "program":

man(socrates). mortal(X) :- man(X).

Page 5: Prolog. Syllogisms “Prolog” is all about programming in logic. –Socrates is a man. –All men are mortal. –Therefore, Socrates is mortal.

Running Prolog II

• Prolog is completely interactive. Begin by– Double-clicking on your .pl file, or– Double-clicking on the SWI-Prolog application and

consulting your file at the prompt:• ?- consult('C:\\My Programs\\adv.pl').

• Then, ask your question at the prompt:– mortal(socrates).

• Prolog responds:– Yes

Page 6: Prolog. Syllogisms “Prolog” is all about programming in logic. –Socrates is a man. –All men are mortal. –Therefore, Socrates is mortal.

Syntax I: Structures

• Example structures:– sunshine– man(socrates)– path(garden, south, sundial)

• <structure> ::= <name> | <name> ( <arguments> )

• <arguments> ::= <argument> | <argument> , <arguments>

Page 7: Prolog. Syllogisms “Prolog” is all about programming in logic. –Socrates is a man. –All men are mortal. –Therefore, Socrates is mortal.

Syntax II: Base Clauses

• Example base clauses:– debug_on.– loves(john, mary).– loves(mary, bill).

• <base clause> ::= <structure> .

Page 8: Prolog. Syllogisms “Prolog” is all about programming in logic. –Socrates is a man. –All men are mortal. –Therefore, Socrates is mortal.

Syntax III: Nonbase Clauses

• Example nonbase clauses:– mortal(X) :- man(X).– mortal(X) :- woman(X)– happy(X) :- healthy(X), wealthy(X), wise(X).

• <nonbase clause> ::= <structure> :- <structures> .

• <structures> ::= <structure> | <structures> , <structure>

Page 9: Prolog. Syllogisms “Prolog” is all about programming in logic. –Socrates is a man. –All men are mortal. –Therefore, Socrates is mortal.

Syntax IV: Predicates

• A predicate is a collection of clauses with the same functor and arity.

• loves(john, mary). loves(mary, bill). loves(chuck, X) :- female(X), rich(X).

• <predicate> ::= <clause> | <predicate> <clause>

• <clause> ::= <base clause> | <nonbase clause>

Page 10: Prolog. Syllogisms “Prolog” is all about programming in logic. –Socrates is a man. –All men are mortal. –Therefore, Socrates is mortal.

Syntax V: Programs

• A program is a collection of predicates.

• Predicates can be in any order.

• Predicates are used in the order in which they occur.

Page 11: Prolog. Syllogisms “Prolog” is all about programming in logic. –Socrates is a man. –All men are mortal. –Therefore, Socrates is mortal.

Syntax VI: Assorted details

• Variables begin with a capital letter: X, Socrates, _result

• Atoms do not begin with a capital letter: x, socrates

• Other atoms must be enclosed in single quotes:– ‘Socrates’– ‘C:/My Documents/examples.pl’

Page 12: Prolog. Syllogisms “Prolog” is all about programming in logic. –Socrates is a man. –All men are mortal. –Therefore, Socrates is mortal.

Syntax VII: Assorted details

• In a quoted atom, a single quote must be quoted or backslashed: 'Can''t, or won\'t?'

• /* Comments are like this */

• Prolog allows some infix operators, such as :- (turnstile) and , (comma). These are syntactic sugar for the functors ':-' and ','.

• Example: ':-'(mortal(X), man(X)).

Page 13: Prolog. Syllogisms “Prolog” is all about programming in logic. –Socrates is a man. –All men are mortal. –Therefore, Socrates is mortal.

Backtracking

• loves(chuck, X) :- female(X), rich(X).• female(jane).• female(mary).• rich(mary).• ---------- Suppose we ask: loves(chuck, X).• female(X) = female(jane), X = jane.• rich(jane) fails.

• female(X) = female(mary), X = mary.• rich(mary) succeeds.

Page 14: Prolog. Syllogisms “Prolog” is all about programming in logic. –Socrates is a man. –All men are mortal. –Therefore, Socrates is mortal.

Additional answers

• female(jane).female(mary).female(susan).

• ?- female(X).• X = jane ;• X = mary• Yes

Page 15: Prolog. Syllogisms “Prolog” is all about programming in logic. –Socrates is a man. –All men are mortal. –Therefore, Socrates is mortal.

Readings

• loves(chuck, X) :- female(X), rich(X).• Declarative reading: Chuck loves X if X is

female and rich.• Approximate procedural reading: To find an

X that Chuck loves, first find a female X, then check that X is rich.

• Declarative readings are almost always preferred.

Page 16: Prolog. Syllogisms “Prolog” is all about programming in logic. –Socrates is a man. –All men are mortal. –Therefore, Socrates is mortal.

Nonmonotonic logic

• Prolog’s facts and rules can be changed at any time.

• assert(man(plato)).• assert((loves(chuck,X) :- female(X), rich(X))).• retract(man(plato)).• retract((loves(chuck,X) :- female(X),

rich(X))).

Page 17: Prolog. Syllogisms “Prolog” is all about programming in logic. –Socrates is a man. –All men are mortal. –Therefore, Socrates is mortal.

Common problems

• Capitalization is extremely important!

• No space between a functor and its argument list: man(socrates), not man (socrates).

• Don’t forget the period! (But you can put it on the next line.)

Page 18: Prolog. Syllogisms “Prolog” is all about programming in logic. –Socrates is a man. –All men are mortal. –Therefore, Socrates is mortal.

Your Assignment

• Your assignment is to write an “adventure game.”

• You need to download SWI-Prolog.

• Use consult/1 to read in a program file:– consult(‘C:/My Documents/examples.pl’).– consult(C:\\My Documents\\examples.pl’).

Page 19: Prolog. Syllogisms “Prolog” is all about programming in logic. –Socrates is a man. –All men are mortal. –Therefore, Socrates is mortal.

Adventure games in Prolog

?- s.You are in a small building. The exit is to the north.There is a barred door to the west, but it seems to beunlocked. There is a smaller door to the east.

There is a flashlight here.

Yes?- take(flashlight).OK.

Yes?-

Page 20: Prolog. Syllogisms “Prolog” is all about programming in logic. –Socrates is a man. –All men are mortal. –Therefore, Socrates is mortal.

Base Clauses

• i_am_at(meadow).• path(spider, d, cave).

path(cave, u, spider).• at(key, cave_entrance).

at(flashlight, building).

Page 21: Prolog. Syllogisms “Prolog” is all about programming in logic. –Socrates is a man. –All men are mortal. –Therefore, Socrates is mortal.

Mixed Base and Nonbase Clauses

• path(building, e, closet) :- at(key, in_hand).

path(building, e, closet) :- write('The door appears to be locked.'), nl, fail.

Page 22: Prolog. Syllogisms “Prolog” is all about programming in logic. –Socrates is a man. –All men are mortal. –Therefore, Socrates is mortal.

Locations

path(cave, w, cave_entrance).path(cave_entrance, e, cave).path(cave_entrance, s, meadow).path(meadow, n, cave_entrance) :- at(flashlight, in_hand).path(meadow, n, cave_entrance) :- write('Go into that dark cave without a light? Are you crazy?'), nl, fail.

Page 23: Prolog. Syllogisms “Prolog” is all about programming in logic. –Socrates is a man. –All men are mortal. –Therefore, Socrates is mortal.

Movement I

?- listing([n,s,e,w]).

n :- go(n).

s :- go(s).

e :- go(e).

w :- go(w).

Page 24: Prolog. Syllogisms “Prolog” is all about programming in logic. –Socrates is a man. –All men are mortal. –Therefore, Socrates is mortal.

Movement II

?- listing(go).

go(A) :- i_am_at(B), path(B, A, C), retract(i_am_at(B)), assert(i_am_at(C)), look.go(A) :- write('You can\'t go that way.').

Page 25: Prolog. Syllogisms “Prolog” is all about programming in logic. –Socrates is a man. –All men are mortal. –Therefore, Socrates is mortal.

Taking objects I

take(A) :- i_am_at(B), at(A, B), retract(at(A, B)), assert(at(A, in_hand)), write('OK.'), nl.

Page 26: Prolog. Syllogisms “Prolog” is all about programming in logic. –Socrates is a man. –All men are mortal. –Therefore, Socrates is mortal.

Taking object II

take(A) :- at(A, in_hand), write('You\'re already holding it!'), nl.take(A) :- (as before).take(A) :- write('I don\'t see it here.'), nl.

Page 27: Prolog. Syllogisms “Prolog” is all about programming in logic. –Socrates is a man. –All men are mortal. –Therefore, Socrates is mortal.

Dropping objects

drop(A) :- at(A, in_hand), i_am_at(B), retract(at(A, in_hand)), assert(at(A, B)), write('OK.'), nl.drop(A) :- write('You aren\'t holding it!'), nl.

Page 28: Prolog. Syllogisms “Prolog” is all about programming in logic. –Socrates is a man. –All men are mortal. –Therefore, Socrates is mortal.

The End


Recommended