260 likes | 389 Views
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.
E N D
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
Chapter 16 Logic Programming Languages
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
Prolog append Function Definition append([],List,List). append([Head|List_1], List_2, [Head|List_3]) :- append(List_1, List_2, List_3).
Prolog reverse Function Definition reverse([],[]). reverse([Head|Tail],List) :- reverse(Tail,Result), append(Result,[Head],List).
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).
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
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
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
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).
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
Prolog Example 2 likes(jake,chocolate). likes(jake,apricots). likes(darcie,licorice). likes(darcie,apricots). trace. likes(jake,X), likes(darcie,X).
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
Prolog append Function Definition append([],List,List). append([Head|List_1], List_2, [Head|List_3]) :- append(List_1, List_2, List_3).
Prolog reverse Function Definition reverse([],[]). reverse([Head|Tail],List) :- reverse(Tail,Result), append(Result,[Head],List).
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).
Prolog member Function Definition member(Element, [Element | _]). member(Element, [_ | List]) :- member(Element, List).
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
Logic ProgrammingDeficiencies Resolution Order Control Closed-World Assumption The Negation Problem Intrinsic Limitations
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
Closed-World Assumption • Prolog cannot prove that a goal is false • True/Fail system, not a True/False system
The Negation Problem Negatives are hard to prove The Horn clause is positive logic B A1A2... An B can be shown to be true, but not false
Intrinsic Limitations Prolog cannot sort well, unless the programmer provides the method New inferencing techniques may correct this
Logic ProgrammingApplications Relational Database Management Systems Expert Systems Natural Language Processing
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