Presentation is loading. Please wait.

Presentation is loading. Please wait.

Prolog Programming in Logic. 2 SWI-Prolog SWI-Prolog is a good, standard Prolog for Windows and Linux Can be installed on Macintosh with a little more.

Similar presentations


Presentation on theme: "Prolog Programming in Logic. 2 SWI-Prolog SWI-Prolog is a good, standard Prolog for Windows and Linux Can be installed on Macintosh with a little more."— Presentation transcript:

1 Prolog Programming in Logic

2 2

3 SWI-Prolog SWI-Prolog is a good, standard Prolog for Windows and Linux Can be installed on Macintosh with a little more effort (requires X11 and Mac developer tools) It's licensed under GPL, therefore free Downloadable from: http://www.swi-prolog.org/ 3

4 Syllogisms “Prolog” is all about programming in logic. Aristotle described syllogisms 2300 years ago Sample syllogism: Socrates is a man. All men are mortal. Therefore, Socrates is mortal. Syllogisms are a form of logic. Can Prolog do them? Note: If a word or term is in red, you should learn and remember its meaning 4

5 Forward and backward reasoning A syllogism gives two premises and a conclusion Forward reasoning: Given some premises, ask “What can we conclude?” Forward reasoning is inefficient when you are trying to get a particular conclusion Backward reasoning: Given some premises and a conjectured conclusion, try to derive the conclusion from the premises You start from the conclusion and try to work backward to prove it You use Prolog by asking it specific questions This is backward reasoning -- from (potential) conclusions to facts 5

6 Syllogisms in Prolog Syllogism Socrates is a man. All men are mortal. Is Socrates mortal? man(socrates). mortal(X) :- man(X). ?- mortal(socrates). Prolog 6

7 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). Queries have the same form as facts 7

8 Variables and atoms Variables begin with a capital letter: X, Socrates, _result A variable can have a value An atom is a value; it just stands for itself Atoms do not begin with a capital letter: x, socrates 8

9 Running Prolog I Create your “database” (program) in any editor Save it as text only, with a.pl extension If you have Perl installed, you may have to use the.pro extension instead Google “ swi prolog file extension” for instructions Here's the complete program: man(socrates). mortal(X) :- man(X). 9

10 Running Prolog II SWI-Prolog is interpreted and completely interactive You may be able to run your program by double-clicking your.pl file Here are two ways you can run the interpreter: Double-click on the swipl file, or If your PATH is set correctly, enter swipl at the command line At the ?- prompt in the interpreter, enter: ?- consult(' Complete path to your.pl file '). Then, ask your question at the prompt: ?- mortal(socrates). Prolog responds: true. 10

11 Prolog is a theorem prover Prolog’s true. means “I can prove it” Prolog’s false. really means “I can’t prove it” It does not mean “I can prove it is untrue.” ?- mortal(plato). false. This is the closed world assumption: the Prolog program knows everything it needs to know Prolog supplies values for variables when it can ?- mortal(X). X = socrates 11

12 Structures A structure consists of a name and zero or more arguments. Omit the parentheses if there are no arguments Example structures: sunshine man(socrates) path(garden, south, sundial) 12

13 Base Clauses A base clause is just a structure, terminated with a period. A base clause represents a simple fact. Example base clauses: debug_on. loves(john, mary). loves(mary, bill). 13

14 Nonbase Clauses A nonbase clause is a structure, a turnstile :- (meaning “if”), and a list of structures. Example nonbase clauses: mortal(X) :- man(X). mortal(X) :- woman(X). happy(X) :- healthy(X), wealthy(X), wise(X). The comma between structures means “and” “X is happy if X is healthy, wealthy, and wise.” 14

15 Predicates A predicate is a collection of clauses with the same functor (name) and arity (number of arguments). loves(john, mary). loves(mary, bill). loves(chuck, X) :- female(X), rich(X). The scope of a variable (such as X ) is the single clause in which it occurs. 15

16 Programs In Prolog, a program is just a collection of predicates. Predicates can be in any order. Clauses within a predicate are used in the order in which they occur. 16

17 Atoms You can make an atom containing any characters at all by enclosing it in single quotes: 'C:\\My Documents\\examples.pl' If you use double quotes, you will get a list of ASCII values ?- X = "Hello". X = [72, 101, 108, 108, 111]. You probably don’t want this! In a quoted atom, a single quote must be doubled or backslashed: 'Can''t, or won\'t?' Backslashes in file names must also be doubled: 'C:\\My Documents\\examples.pl' Better yet, use forward slashes in paths; every OS, including Windows, understands this 17

18 Common problems Capitalization is meaningful! No space is allowed between a functor and its argument list: man(socrates), not man (socrates). Double quotes indicate a list of ASCII character values, not a string Don’t forget the period! (But if you do, you can put it on the next line.) 18

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

20 Backtracking and Beads Each Prolog call is like a “bead” in a string of beads: call fail exit redo Each structure has four ports: call, exit, redo, fail Exit ports connect to call ports; fail ports connect to redo ports 20

21 Calls as nested beads loves(chuck, X) :- female(X), rich(X). loves(chuck, X) female(X)rich(X) call fail exit redo 21

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

23 Negation Since Prolog is programming in logic, you would expect it to support negation (the “not” operator) It does (the operator is \+ ), but negation isn’t as useful as you might think Consider \+loves(chuck, X) You might think that this would instantiate X with a value that chuck doesn’t love Here’s what really happens: Prolog tries to find a solution (an X ) for loves(chuck, X) If loves(chuck, X) succeeds, \+loves(chuck, X) fails, so the instantiation for X is discarded If loves(chuck, X) fails, there is no instantiation for X, but \+loves(chuck, X) succeeds Hence, whether the test succeeds or fails, X doesn’t get a value 23

24 Readings A clause can be read declaratively (as a statement of fact) or procedurally (as a list of things to try to do) 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 try to find a female X (fail and backtrack if you can’t) Given a particular value for X, try to show that X is rich (fail and backtrack if you can’t) Declarative readings are almost always preferred. 24

25 The End that that is is that that is not is not is not that so That that is, is; that that is not, is not. Is not that so? 25


Download ppt "Prolog Programming in Logic. 2 SWI-Prolog SWI-Prolog is a good, standard Prolog for Windows and Linux Can be installed on Macintosh with a little more."

Similar presentations


Ads by Google