Prolog Programming in Logic 2 SWIProlog n n
- Slides: 30
Prolog Programming in Logic
2
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 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 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) : - man(X). Is Socrates mortal? ? - mortal(socrates). 6
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 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 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 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’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
Given this (complete) program: man(socrates). What will Prolog respond if you ask it ? - man(Aristotle). A. true. B. false. C. Aristotle = socrates. 12
Given this program: man(socrates). mortal(X) : - man(X). What will Prolog respond if you ask it ? - mortal(Aristotle). A. true. B. false. C. Aristotle = socrates. 13
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) 14
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). 15
What does this base clause mean? loves(john, mary). A. John loves Mary. B. Mary loves John. C. Whatever you want it to mean. 20 16
If we decide that loves(john, mary). means “John loves Mary, ” then what does loves(mary, Bill). mean? A. Bill loves Mary. B. Mary loves Bill. C. Mary loves everybody and everything. 20 17
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. ” 18
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. 19
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. 20
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 21
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. ) 22
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. 23
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 24
Calls as nested beads loves(chuck, X) : - female(X), rich(X). loves(chuck, X) call fail female(X) rich(X) exit redo 25
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) 26
Given this program: father(stan, chester). father(chester, david). father(david, steven). father(steven, isaac). ancestor(X, Y) : - father(X, Y). ancestor(X, Y) : - ancestor(X, Z), father(Z, Y). How will Prolog respond to: ? - ancestor(X, isaac). A. The first answer will be X = steven B. The first answer will be X = stan C. Prolog will return a list of all ancestors of isaac 20 27
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 28
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. 29
The End that is is that is not that so That that is, is; that is not, is not. Is not that so? 30
- Swiprolog
- Cryptarithmetic problem logic+logic=prolog
- Visual prolog
- Visual prolog vs swi-prolog
- Propositional logic in prolog
- Prolog programming
- Logic programming language
- Logic chapter three
- First order logic vs propositional logic
- Combinational logic sequential logic 차이
- First order logic vs propositional logic
- Majority circuit
- First order logic vs propositional logic
- Combinational logic sequential logic 차이
- Concurrent vs sequential
- Logic programming tutorial
- Automated logic programming
- Logic programming
- Loop logic programming
- Abductive logic programming alp
- Plc ladder
- What is visual logic
- Induction reasoning
- Programming logic exercises
- System programming vs application programming
- Integer programming vs linear programming
- Perbedaan linear programming dan integer programming
- Programing adalah
- Greedy vs dynamic programming
- Unification prolog
- Bfs in prolog