1 About Prolog Lu Han Written at 2007-08 Modified at 2008-02.

46
1 About Prolog Lu Han Written at 2007-08 Modified at 2008-02

Transcript of 1 About Prolog Lu Han Written at 2007-08 Modified at 2008-02.

Page 1: 1 About Prolog Lu Han Written at 2007-08 Modified at 2008-02.

1

About Prolog

Lu HanWritten at 2007-08Modified at 2008-02

Page 2: 1 About Prolog Lu Han Written at 2007-08 Modified at 2008-02.

2

Index

Part 1. Prolog Quick Tutorial Part 2. Amzi! Prolog Overview Part 3. Programming Interface ( for

C & C++ )

Page 3: 1 About Prolog Lu Han Written at 2007-08 Modified at 2008-02.

3

Part 1. Prolog Quick Tutorial What is Prolog ? Prolog stands for PROgramming in LOGic Prolog is based on First Order Predict Logic. Prolog is declarative rather than procedural.

Page 4: 1 About Prolog Lu Han Written at 2007-08 Modified at 2008-02.

4

Part 1. Prolog Quick Tutorial Logic Programming Sample 1. In classical logic we might say "All people are mort

al," or, rephrased for Prolog, "For all X, X is mortal if X is a person."

mortal(X) :- person(X).

Page 5: 1 About Prolog Lu Han Written at 2007-08 Modified at 2008-02.

5

Part 1. Prolog Quick Tutorial Similarly, we can assert the simple fact that Socrat

es is a person. person(socrates). From these two logical assertions, Prolog can now

prove whether or not Socrates is mortal. ?- mortal(socrates). The listener responds yes

Page 6: 1 About Prolog Lu Han Written at 2007-08 Modified at 2008-02.

6

Part 1. Prolog Quick Tutorial First Order Predicate Logic ( FOPL ) First order predicate logic implies the exi

stence of a set of predicate symbols along with a set of connectives.

First order predicate logic implies that there is no means provided for “talking about” the predicates themselves.

Sample 2. x( ( ) ( ))person x mortal x

Page 7: 1 About Prolog Lu Han Written at 2007-08 Modified at 2008-02.

7

Part 1. Prolog Quick Tutorial Procedural programming requires

that the programmer tell the computer what to do. That is, how to get the output for the given inputs.

Declarative programming requires a more descriptive style. The programmer must know what relationships hold between various entities.

Page 8: 1 About Prolog Lu Han Written at 2007-08 Modified at 2008-02.

8

Part 1. Prolog Quick Tutorial

Prolog Constants A constant is an atom or a number. Atom include:

Quoted item Word ( lower case letter followed by any letter , digit or

_ ) Symbol ( +, -, *, /, ^…… ) Special item ( [], {}, ;, !, % )

A number is an integer or a real number. Sample 3. a, b_1, ‘Hello’, ‘ 常识组’ , 2008, 3.14

Page 9: 1 About Prolog Lu Han Written at 2007-08 Modified at 2008-02.

9

Part 1. Prolog Quick Tutorial The Logical Variables A logical variable is a name starting

with a capital letter or a single _. Sample 4. X, Person, _

Page 10: 1 About Prolog Lu Han Written at 2007-08 Modified at 2008-02.

10

Part 1. Prolog Quick Tutorial Predicates Predicate means the relation or the propert

y of a number of entities. All predicate names must be constants, but

not numbers. No predicate may be a variable. Prolog use /num to indicate the number of

arguments Sample 5. likes(bill,ice_cream) —— right. likes/2

Page 11: 1 About Prolog Lu Han Written at 2007-08 Modified at 2008-02.

11

Part 1. Prolog Quick Tutorial Clauses A clause is the syntactic entity expressing a

relationship. A clause must terminate with a ‘.’ Sample 6. loves(jane,jim) —— a goal loves(jane,jim). —— a clause

Page 12: 1 About Prolog Lu Han Written at 2007-08 Modified at 2008-02.

12

Part 1. Prolog Quick Tutorial

Multiple Clauses A predicate may be defined by a set of

clauses with the same predicate name and the same number of arguments.

Sample 7. The logical statement large_than(2,1) ∧large_than(3,2) Prolog clauses large_than(2,1). large_than(3,2).

Page 13: 1 About Prolog Lu Han Written at 2007-08 Modified at 2008-02.

13

Part 1. Prolog Quick Tutorial Facts A fact is a unit clause. Sample 8 likes(bill,cake). even(2).

Page 14: 1 About Prolog Lu Han Written at 2007-08 Modified at 2008-02.

14

Part 1. Prolog Quick Tutorial Rule A rule is a non-unit clause. A rule consists of two parts: the head and the

body. No more than one goal is allowed in the head. The head and the body are connected with

“:-”. The semantics of goal(X) :- p(X). is for all X, p

(X) goal(X) Sample 9. father(X, Y) :- son(Y, X).

Page 15: 1 About Prolog Lu Han Written at 2007-08 Modified at 2008-02.

15

Part 1. Prolog Quick Tutorial Conjunctions Prolog uses ‘,’ to indicate the conjunctio

n, which equivalent to the ∧ of predicate calculus.

Sample 10. happy(Person) :- rich(Person), healthy(Person). The rule means a person is happy if he/she i

s rich and healthy.

Page 16: 1 About Prolog Lu Han Written at 2007-08 Modified at 2008-02.

16

Part 1. Prolog Quick Tutorial Disjunctions Prolog uses ‘;’ to indicate the conjunction, whic

h equivalent to the ∨ of predicate calculus. Sample 11. happy(Person) :- rich(Person); healthy(Person). Or happy(Person) :- rich(Person). happy(Person) :- healthy(Person). The rule means a person is happy if he/she is rich o

r healthy.

Page 17: 1 About Prolog Lu Han Written at 2007-08 Modified at 2008-02.

17

Part 1. Prolog Quick Tutorial Recursion in rule ancestor(P, A) :-

ancestor(P, X), ancestor(X, A). —— Wrong! This rule should be written as follow. ancestor(P, A) :- parent(P, A). ancestor(P, A) :- parent(P, P1), ancestor(P1, A).

Page 18: 1 About Prolog Lu Han Written at 2007-08 Modified at 2008-02.

18

Part 1. Prolog Quick Tutorial Search Strategy Prolog adopts depth first search as its

search strategy. A query is a goal which is submitted to

Prolog in order to determine whether this goal is true or false.

Prolog uses the prompt ‘?-’ to expect the input queries.

Page 19: 1 About Prolog Lu Han Written at 2007-08 Modified at 2008-02.

19

Part 1. Prolog Quick Tutorial Unifications The predicate names and arities must be th

e same for unification to succeed. Sample 12 man(bill). woman(jean). ?-woman(jean). yes —— unify with woman(jean) ?-women(mary). no —— a failed match

Page 20: 1 About Prolog Lu Han Written at 2007-08 Modified at 2008-02.

20

Part 1. Prolog Quick Tutorial Subgoals A top level goal can be divided into several s

ubgoals. Sample 13 woman(jean). man(fred). wealthy(fred). happy(Person) :- woman(Person), wealthy(Person). ?-happy(jean). no

Page 21: 1 About Prolog Lu Han Written at 2007-08 Modified at 2008-02.

21

Part 1. Prolog Quick Tutorial happy(jean) :- woman(jean), wealthy(jean). Then there are two subgoals, woman(jean) wealthy(jean) Prolog solves the two subgoals in order. The former subgoal is successful. But we cannot unify wealthy(fred) with wealthy(jea

n), so happy(jean) is failed.

Page 22: 1 About Prolog Lu Han Written at 2007-08 Modified at 2008-02.

22

Part 1. Prolog Quick Tutorial Backtracking When one goal failed, Prolog moves back (b

acktracking) to next goal. Sample 14. man(john). woman(jean). ?-woman(Person). Person = jean

Page 23: 1 About Prolog Lu Han Written at 2007-08 Modified at 2008-02.

23

Part 1. Prolog Quick Tutorial Predicates for control true/0 —— always true father(jim,fred). Is logically equivalent to father(jim,fred) :- true. fail/0 —— always false live_forever(Person) :- fail. repeat/0 —— redo test :- repeat, write(test), fail.

Page 24: 1 About Prolog Lu Han Written at 2007-08 Modified at 2008-02.

24

Part 1. Prolog Quick Tutorial Negation Prolog uses predicate \+/1 to indicate not

(equivalent to ┐) The predicate \+/1 takes a Prolog goal as its

argument. It will succeed when the goal fails.

Sample 15 man(jim). ?-\+man(jim). no

Page 25: 1 About Prolog Lu Han Written at 2007-08 Modified at 2008-02.

25

Part 1. Prolog Quick Tutorial \+/1 is not at all like logical negation. \+(\+goal) is not identical to goal. Sample 16 man(jim). woman(X) :- \+(man(X)). ?-woman(jane). yes ?-woman(Person).//zsd 似乎所有的常量都代入成功

才行。 no

Page 26: 1 About Prolog Lu Han Written at 2007-08 Modified at 2008-02.

26

Part 1. Prolog Quick Tutorial Using Negation in Case selection Sample 17 goal_1 :- odd(X). goal_2 :- \+odd(X).

Page 27: 1 About Prolog Lu Han Written at 2007-08 Modified at 2008-02.

27

Part 1. Prolog Quick Tutorial Condition Prolog uses test-process to guarantee that s

ome process will be done only when conditions are satisfied.

test_process(Cond, X, Y) :-test(Cond),process(Cond, X, Y).

Page 28: 1 About Prolog Lu Han Written at 2007-08 Modified at 2008-02.

28

Part 1. Prolog Quick Tutorial Sample 18 p(Person, Age) :-

Age < 20, write(Person), write(‘ is young’);Age == 20, write(Person), write(‘ is 20’);Age >=60, write(Person), write(‘ is old’).

Page 29: 1 About Prolog Lu Han Written at 2007-08 Modified at 2008-02.

29

Part 1. Prolog Quick Tutorial If _then_else Prolog uses (test)->process1;process2 as if_

then_else. Sample 19 P2(Person) :-

(man(Person)->write(Person), write(‘ is male’);write(Person), write(‘ is female’)).

Page 30: 1 About Prolog Lu Han Written at 2007-08 Modified at 2008-02.

30

Part 1. Prolog Quick Tutorial

Failure-Driven Loop//???zsd The idea of failure-driven loop is deliberatel

y generate a term and then fail. failure_drivern_loop(Info) :-

generate(Info,Term),fail.

failure_drivern_loop(Info).

Page 31: 1 About Prolog Lu Han Written at 2007-08 Modified at 2008-02.

31

Part 1. Prolog Quick Tutorial Sample 20 int(1). int(2). int(3). print_int :- int(X), write(X), fail. print_int. ?-print_int. 123 yes

Page 32: 1 About Prolog Lu Han Written at 2007-08 Modified at 2008-02.

32

Part 2. Amzi! Prolog Overview Amzi! Prolog consists of Amzi! Prolog and lo

gic server. It is provided by Amzi! Inc. www.amzi.com Editions: Free, Personal/Student, Develope

r, Professional, Enterprise

Page 33: 1 About Prolog Lu Han Written at 2007-08 Modified at 2008-02.

33

Part 2. Amzi! Prolog Overview Amzi! Prolog IDE

Page 34: 1 About Prolog Lu Han Written at 2007-08 Modified at 2008-02.

34

Part 2. Amzi! Prolog Overview New a Project File -> new -> Project…-> Prolog

project -> Project name -> Create a new Prolog project

New a File File -> new -> File -> Direction select ->

File name(*.pro) -> Create a new file

Page 35: 1 About Prolog Lu Han Written at 2007-08 Modified at 2008-02.

35

Part 2. Amzi! Prolog Overview Document composition Logical Layer —— rule files Data Layer —— fact files Control layer —— main function,

input/output etc.

Page 36: 1 About Prolog Lu Han Written at 2007-08 Modified at 2008-02.

36

Part 2. Amzi! Prolog Overview Debug Run -> Debug As… Run Run -> Run As ->

1 Compiled Project (xpl) 2 Interpreted Project (pro) 3 Interpreted Single File (pro)

Page 37: 1 About Prolog Lu Han Written at 2007-08 Modified at 2008-02.

37

Part 2. Amzi! Prolog Overview Listener The Prolog listener is an interactive

environment that allows a programmer to query any part of the Prolog logic base, or program.

Page 38: 1 About Prolog Lu Han Written at 2007-08 Modified at 2008-02.

38

Part 2. Amzi! Prolog Overview Adding and Removing Clauses Assert can be used to directly add clauses. ?- assert( likes(ella, crackers) ). yes Retractall ca be used to remove clauses. ?-retractall( likes(ella, crackers) ). yes

Page 39: 1 About Prolog Lu Han Written at 2007-08 Modified at 2008-02.

39

Part 2. Amzi! Prolog Overview The listener attempts to prove the goal,

returning values of any variables. You can then enter:

; - request backtracking to look for more answers.

Enter key - to return to the listener prompt.

Page 40: 1 About Prolog Lu Han Written at 2007-08 Modified at 2008-02.

40

Part 2. Amzi! Prolog Overview Consulting Prolog Source Files consult(File) - the specified file is consulted.

If no extension is provided, then '.pro' is assumed.

consult( [File1, File2, ..., FileN] ) - consults each of the files in the list.

Page 41: 1 About Prolog Lu Han Written at 2007-08 Modified at 2008-02.

41

Part 2. Amzi! Prolog Overview Listing Clauses The listing predicate displays a listing of

your clauses. ?-listing. Exits the current listener. ?-quit.

Page 42: 1 About Prolog Lu Han Written at 2007-08 Modified at 2008-02.

42

Part 3. Programming Interface

1. Install Amzi Prolog Inference Engine (PIE) in your machine.

2. Include amzi.h and logicserver.h in your VC project. (..\amzi\amzi_7-6-6\include\)

3. Include amzi.lib in your VC project. (..\amzi\amzi_7-6-6\lib\)

Project -> settings… -> Link -> Object/library modules -> add amzi.lib

Page 43: 1 About Prolog Lu Han Written at 2007-08 Modified at 2008-02.

43

Part 3. Programming Interface

4. Include prolog.h (my VC Interface Class) in your VC project.

class Prolog Prolog( string filename ); void Load( string filename); void Assert( string str ); void Retract( string filename); VSTR Prolog::Call( string command ); VSTR GetPar( string command, int n_Par);

Page 44: 1 About Prolog Lu Han Written at 2007-08 Modified at 2008-02.

44

Part 3. Programming Interface

5. Generate .xpl in your PIE. Run -> Run As… -> 1 Compiled Project (xpl)

6. Copy the xpl file to your VC project.7. Call Prolog via class Prolog.8. Run your project.

Page 45: 1 About Prolog Lu Han Written at 2007-08 Modified at 2008-02.

45

Part 3. Programming Interface Another way for call Prolog main.xpl cmd -> main Rule, Data, Query, Output

Page 46: 1 About Prolog Lu Han Written at 2007-08 Modified at 2008-02.

46

Thank You!

Question and discussion are welcome.