Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Prolog. Syllogisms “Prolog” is all about programming in logic. –Socrates is a man. –All men are mortal. –Therefore, Socrates is mortal."— Presentation transcript:

1 Prolog

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

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

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

5 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

6 Syntax I: Structures Example structures: –sunshine –man(socrates) –path(garden, south, sundial) ::= | ( ) ::= |,

7 Syntax II: Base Clauses Example base clauses: –debug_on. –loves(john, mary). –loves(mary, bill). ::=.

8 Syntax III: Nonbase Clauses Example nonbase clauses: –mortal(X) :- man(X). –mortal(X) :- woman(X) –happy(X) :- healthy(X), wealthy(X), wise(X). ::= :-. ::= |,

9 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). ::= |

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

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

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

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

14 Additional answers female(jane). female(mary). female(susan). ?- female(X). X = jane ; X = mary Yes

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

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

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

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

19 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 be unlocked. There is a smaller door to the east. There is a flashlight here. Yes ?- take(flashlight). OK. Yes ?-

20 Base Clauses i_am_at(meadow). path(spider, d, cave). path(cave, u, spider). at(key, cave_entrance). at(flashlight, building).

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

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

23 Movement I ?- listing([n,s,e,w]). n :- go(n). s :- go(s). e :- go(e). w :- go(w).

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

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

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

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

28 The End


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

Similar presentations


Ads by Google