1 / 46

About Prolog

About Prolog. Lu Han Written at 2007-08 Modified at 2008-02. Index. Part 1. Prolog Quick Tutorial Part 2. Amzi! Prolog Overview Part 3. Programming Interface ( for C & C++ ). Part 1. Prolog Quick Tutorial. What is Prolog ? Prolog stands for PRO gramming in LOG ic

jola
Download Presentation

About Prolog

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  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++)

  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.

  4. Part 1. Prolog Quick Tutorial • Logic Programming • Sample 1. • In classical logic we might say "All people are mortal," or, rephrased for Prolog, "For all X, X is mortal if X is a person." • mortal(X) :- person(X).

  5. Part 1. Prolog Quick Tutorial • Similarly, we can assert the simple fact that Socrates 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

  6. Part 1. Prolog Quick Tutorial • First Order Predicate Logic(FOPL) • First order predicate logic implies the existence 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.

  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.

  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

  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, _

  10. Part 1. Prolog Quick Tutorial • Predicates • Predicate means the relation or the property 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

  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

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

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

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

  15. Part 1. Prolog Quick Tutorial • Conjunctions • Prolog uses ‘,’ to indicate the conjunction, 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 is rich and healthy.

  16. Part 1. Prolog Quick Tutorial • Disjunctions • Prolog uses ‘;’ to indicate the conjunction, which 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 or healthy.

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

  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.

  19. Part 1. Prolog Quick Tutorial • Unifications • The predicate names and arities must be the same for unification to succeed. • Sample 12 • man(bill). • woman(jean). • ?-woman(jean). • yes —— unify with woman(jean) • ?-women(mary). • no —— a failed match

  20. Part 1. Prolog Quick Tutorial • Subgoals • A top level goal can be divided into several subgoals. • Sample 13 • woman(jean). • man(fred). • wealthy(fred). • happy(Person) :- woman(Person), wealthy(Person). • ?-happy(jean). • no

  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(jean), so happy(jean) is failed.

  22. Part 1. Prolog Quick Tutorial • Backtracking • When one goal failed, Prolog moves back (backtracking) to next goal. • Sample 14. • man(john). • woman(jean). • ?-woman(Person). • Person = jean

  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.

  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

  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

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

  27. Part 1. Prolog Quick Tutorial • Condition • Prolog uses test-process to guarantee that some process will be done only when conditions are satisfied. • test_process(Cond, X, Y) :- test(Cond), process(Cond, X, Y).

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

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

  30. Part 1. Prolog Quick Tutorial • Failure-Driven Loop//???zsd • The idea of failure-driven loop is deliberately generate a term and then fail. • failure_drivern_loop(Info) :- generate(Info,Term), fail. • failure_drivern_loop(Info).

  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

  32. Part 2. Amzi! Prolog Overview • Amzi! Prolog consists of Amzi! Prolog and logic server. It is provided by Amzi! Inc. • www.amzi.com • Editions: Free, Personal/Student, Developer, Professional, Enterprise

  33. Part 2. Amzi! Prolog Overview • Amzi! Prolog IDE

  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

  35. Part 2. Amzi! Prolog Overview • Document composition • Logical Layer —— rule files • Data Layer —— fact files • Control layer —— main function, input/output etc.

  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)

  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.

  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

  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.

  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.

  41. Part 2. Amzi! Prolog Overview • Listing Clauses • The listing predicate displays a listing of your clauses. • ?-listing. • Exits the current listener. • ?-quit.

  42. Part 3. Programming Interface • Install Amzi Prolog Inference Engine (PIE) in your machine. • Include amzi.h and logicserver.h in your VC project. (..\amzi\amzi_7-6-6\include\) • Include amzi.lib in your VC project. (..\amzi\amzi_7-6-6\lib\) • Project -> settings… -> Link -> Object/library modules -> add amzi.lib

  43. Part 3. Programming Interface • 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);

  44. Part 3. Programming Interface • Generate .xpl in your PIE. • Run -> Run As… -> 1 Compiled Project (xpl) • Copy the xpl file to your VC project. • Call Prolog via class Prolog. • Run your project.

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

  46. Thank You! Question and discussion are welcome.

More Related