Prolog Programming in Logic 2 SWIProlog n n

  • Slides: 25
Download presentation
Prolog Programming in Logic

Prolog Programming in Logic

2

2

SWI-Prolog n n SWI-Prolog is a good, standard Prolog for Windows and Linux Can

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

Syllogisms n n n “Prolog” is all about programming in logic. Aristotle described syllogisms

Syllogisms n n n “Prolog” is all about programming in logic. Aristotle described syllogisms 2300 years ago Sample syllogism: n n n 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

Forward and backward reasoning n n A syllogism gives two premises and a conclusion

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

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

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

Facts, rules, and queries n n n n Fact: Socrates is a man(socrates). Rule:

Facts, rules, and queries n n n n Fact: Socrates is a man(socrates). Rule: All men are mortal(X) : - man(X). Query: Is Socrates mortal? mortal(socrates). Queries have the same form as facts 7

Variables and atoms n n Variables begin with a capital letter: X, Socrates, _result

Variables and atoms n n 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

Running Prolog I n n Create your “database” (program) in any editor Save it

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

Running Prolog II n n n SWI-Prolog is interpreted and completely interactive You may

Running Prolog II n n n 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: n n n 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: n n Then, ask your question at the prompt: n n ? - consult('Complete path to your. pl file'). ? - mortal(socrates). Prolog responds: n true. 10

Prolog is a theorem prover n n Prolog’s true. means “I can prove it”

Prolog is a theorem prover n n Prolog’s true. means “I can prove it” Prolog’s false. really means “I can’t prove it” n n 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 n ? - mortal(X). X = socrates 11

Structures n n n A structure consists of a name and zero or more

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

Base Clauses n n n A base clause is just a structure, terminated with

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

Nonbase Clauses n n A nonbase clause is a structure, a turnstile “if”), and

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

Predicates n n n A predicate is a collection of clauses with the same

Predicates n n n 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

Programs n n n In Prolog, a program is just a collection of predicates.

Programs n n n 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

Atoms n You can make an atom containing any characters at all by enclosing

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

Common problems n n Capitalization is meaningful! No space is allowed between a functor

Common problems n n 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

Backtracking n n n loves(chuck, X) : - female(X), rich(X). female(jane). female(mary). rich(mary). -----

Backtracking n n n loves(chuck, X) : - female(X), rich(X). female(jane). female(mary). rich(mary). ----- Suppose we ask: loves(chuck, X). n female(X) = female(jane), X = jane. n rich(jane) fails. n female(X) = female(mary), X = mary. n rich(mary) succeeds. 19

Backtracking and Beads n Each Prolog call is like a “bead” in a string

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

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

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

Additional answers n n n female(jane). female(mary). female(susan). ? - female(X). X = jane

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

Negation n Since Prolog is programming in logic, you would expect it to support

Negation n Since Prolog is programming in logic, you would expect it to support negation (the “not” operator) n n It does (the operator is +), but negation isn’t as useful as you might think Consider +loves(chuck, X) n n You might think that this would instantiate X with a value that chuck doesn’t love Here’s what really happens: n n 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

Readings n n A clause can be read declaratively (as a statement of fact)

Readings n n 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: n n n 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

The End that is is that is not that so That that is, is;

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