1 / 26

CMP 339/692 Programming Languages Day 17 Thursday, March 29, 2012

CMP 339/692 Programming Languages Day 17 Thursday, March 29, 2012. Rhys Eric Rosholt. Office: Office Phone: Web Site: Email Address:. Gillet Hall - Room 304 718-960-8663 http://comet.lehman.cuny.edu/rosholt/ rhys.rosholt @ lehman.cuny.edu. Chapter 16. Logic Programming Languages.

phyre
Download Presentation

CMP 339/692 Programming Languages Day 17 Thursday, March 29, 2012

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. CMP 339/692Programming LanguagesDay 17Thursday,March 29, 2012 Rhys Eric Rosholt Office: Office Phone: Web Site: Email Address: Gillet Hall - Room 304 718-960-8663 http://comet.lehman.cuny.edu/rosholt/ rhys.rosholt @ lehman.cuny.edu

  2. Chapter 16 Logic Programming Languages

  3. Chapter 16 Topics Introduction A Brief Introduction to Predicate Calculus Predicate Calculus and Proving Theorems An Overview of Logic Programming The Origins of Prolog The Basic Elements of Prolog Deficiencies of Prolog Applications of Logic Programming

  4. Prolog append Function Definition append([],List,List). append([Head|List_1], List_2, [Head|List_3]) :- append(List_1, List_2, List_3).

  5. Prolog reverse Function Definition reverse([],[]). reverse([Head|Tail],List) :- reverse(Tail,Result), append(Result,[Head],List).

  6. Rule Statements parent(kim,kathy):- mother(kim,kathy). Can use variables (universal objects) to generalize meaning: parent(X,Y) :- mother(X,Y). grandparent(X,Z) :- parent(X,Y), parent(Y,Z). sibling(X,Y) :- mother(M,X), mother(M,Y), father(F,X), father(F,Y).

  7. Inferencing Process of Prolog Queries are called goals If a goal is a compound proposition, each of the facts is a subgoal To prove a goal is true, must find a chain of inference rules and/or facts. For goal Q: B :- A C :- B ... Q :- P Process of proving a subgoal is called matching, satisfying, or resolution

  8. Inferencing Process When goal has more than one subgoal, it is possible to use either Depth-first search find a complete proof for the first subgoal before working on others Breadth-first search work on all subgoals in parallel Prolog uses depth-first search Requires fewer computer resources

  9. Trace Built-in structure that displays instantiations at each step Tracing model of execution - four events: Call beginning of attempt to satisfy goal Exit when a goal has been satisfied Redo when backtrack occurs Fail when goal fails

  10. Trace Model

  11. Prolog Example 1 speed(ford,100). speed(chevy,105). speed(dodge,95). speed(volvo,80). time(ford,20). time(chevy,21). time(dodge,24). time(volvo,24). distance(X,Y) :- speed(X,Speed), time(X,Time), Y is Speed * Time. distance(chevy, Chevy_Distance).

  12. Prolog Example 1 trace. distance(chevy, Chevy_Distance). (1) 1 Call: distance(chevy, _0)? (2) 2 Call: speed(chevy, _5)? (2) 2 Exit: speed(chevy, 105) (3) 2 Call: time(chevy, _6)? (3) 2 Exit: time(chevy, 21)? (4) 2 Call: _0 is 105*21? (4) 2 Exit: 2205 is 105*21 (1) 1 Exit: distance(chevy, 2205) Chevy_Distance = 2205

  13. Prolog Example 2 likes(jake,chocolate). likes(jake,apricots). likes(darcie,licorice). likes(darcie,apricots). trace. likes(jake,X), likes(darcie,X).

  14. Prolog Example 2 trace. likes(jake,X), likes(darcie,X). (1) 1 Call: likes(jake, _0)? (1) 1 Exit: likes(jake, chocolate)? (2) 1 Call: likes(darcie, chocolate)? (2) 1 Fail: likes(darcie, chocolate)? (1) 1 Redo: likes(jake, _0)? (1) 1 Exit: likes(jake, apricots)? (3) 1 Call: likes(darcie, apricots)? (3) 1 Exit: likes(darcie, apricots)? X = apricots

  15. Prolog append Function Definition append([],List,List). append([Head|List_1], List_2, [Head|List_3]) :- append(List_1, List_2, List_3).

  16. Prolog reverse Function Definition reverse([],[]). reverse([Head|Tail],List) :- reverse(Tail,Result), append(Result,[Head],List).

  17. Prolog reverse Function Definition % Here is a more efficient (iterative/tail recursive) version. reverse([],[]). reverse(L,RL) :- reverse(L,[],RL). reverse([],RL,RL). reverse([X|L],PRL,RL) :- reverse(L,[X|PRL],RL).

  18. Prolog member Function Definition member(Element, [Element | _]). member(Element, [_ | List]) :- member(Element, List).

  19. Logic ProgrammingAdvantages • Prolog programs are based on logic • they are likely to be more logically organized and written • Processing is naturally parallel • Prolog interpreters can take advantage of multi-processor machines • Programs are concise • development time is decreased • good for prototyping

  20. Logic ProgrammingDeficiencies Resolution Order Control Closed-World Assumption The Negation Problem Intrinsic Limitations

  21. Resolution Order Control • Rule order (clause order) determines the order in which solutions are found • Goal order determines the search tree • Prolog uses Left-to-Right Resolution • Left recursion causes infinite loops • Same problems as Recursive Descent • Special explicit control of backtracking • “Cut” operator ( ! ) • A goal that is always satisfied • Cannot be resatisfied after backtracking

  22. Closed-World Assumption • Prolog cannot prove that a goal is false • True/Fail system, not a True/False system

  23. The Negation Problem Negatives are hard to prove The Horn clause is positive logic B A1A2... An B can be shown to be true, but not false

  24. Intrinsic Limitations Prolog cannot sort well, unless the programmer provides the method New inferencing techniques may correct this

  25. Logic ProgrammingApplications Relational Database Management Systems Expert Systems Natural Language Processing

  26. Next ClassTuesdayApril 3, 2012 Rhys Eric Rosholt Office: Office Phone: Web Site: Email Address: Gillet Hall - Room 304 718-960-8663 http://comet.lehman.cuny.edu/rosholt/ rhys.rosholt @ lehman.cuny.edu

More Related