CSE 452: Programming Languages Logical Programming Languages Part 2.

Slides:



Advertisements
Similar presentations
COMMONWEALTH OF AUSTRALIA Copyright Regulations 1969 WARNING This material has been reproduced and communicated to you by or on behalf of Monash University.
Advertisements

1 Introduction to Prolog References: – – Bratko, I., Prolog Programming.
1 Logic Programming. 2 A little bit of Prolog Objects and relations between objects Facts and rules. Upper case are variables. parent(pam, bob).parent(tom,bob).
INTRODUCTION TO PROLOG. PROLOG BASICS Atoms - most primitive terms that the language manipulates start with lower case letter includes strings (‘inside.
About prolog  History  Symbolic Programming Language  Logic Programming Language  Declarative Programming Language.
CS 330 Programming Languages 12 / 02 / 2008 Instructor: Michael Eckmann.
Logic Programming Languages featuring Prolog, your new favorite language.
CSE 425: Logic Programming I Logic and Programs Most programs use Boolean expressions over data Logic statements can express program semantics –I.e., axiomatic.
ISBN Chapter 16 Logic Programming Languages.
CS 330 Programming Languages 12 / 12 / 2006 Instructor: Michael Eckmann.
For Friday Read “lectures” 1-5 of Learn Prolog Now: prolog-now/
Chapter 12: Expert Systems Design Examples
Programming Languages Third Edition
(9.1) COEN Logic Programming  Logic programming and predicate calculus  Prolog statements  Facts and rules  Matching  Subgoals and backtracking.
Logic Programming Tasanawan Soonklang. Programming paradigms Imperative Object-oriented Functional Logic Procedural programming Non-procedural programming.
ISBN Chapter 16 Logic Programming Languages.
Logic Programming Languages
Data Structures Using C++ 2E Chapter 6 Recursion.
CPS 506 Comparative Programming Languages
Formal Models of Computation Part II The Logic Model
Notes for Chapter 12 Logic Programming The AI War Basic Concepts of Logic Programming Prolog Review questions.
Data Structures Using C++ 2E Chapter 6 Recursion.
CS 330 Programming Languages 12 / 04 / 2007 Instructor: Michael Eckmann.
Chapter 16 Logic Programming Languages. Copyright © 2012 Addison-Wesley. All rights reserved.1-2 Chapter 16 Topics Introduction A Brief Introduction to.
1-1 Introduction Logic programming languages, sometimes called declarative programming languages Express programs in a form of symbolic logic Use a logical.
14/10/04 AIPP Lecture 7: The Cut1 Controlling Backtracking: The Cut Artificial Intelligence Programming in Prolog Lecturer: Tim Smith Lecture 7 14/10/04.
1 COSC3306: Programming Paradigms Lecture 8: Declarative Programming Specifications Haibin Zhu, Ph.D. Computer Science Nipissing University (C) 2003.
Introduction to Prolog Asst. Prof. Dr. Senem Kumova Metin Revised lecture notes of “Concepts of Programmig Languages, Robert W. Sebesta, Ch. 16”
For Wednesday Read “lectures” 7-10 of Learn Prolog Now Chapter 9, exs 4 and 6. –6 must be in Horn clause form Prolog Handout 2.
Introduction To PROLOG World view of imperative languages. World view of relational languages. A PROLOG program. Running a PROLOG program. A PROLOG.
CHAPTER 15 & 16 Functional & Logic Programming Languages.
1 Knowledge Based Systems (CM0377) Lecture 3 (Last modified 5th February 2001)
1 Prolog and Logic Languages Aaron Bloomfield CS 415 Fall 2005.
The AI War LISP and Prolog Basic Concepts of Logic Programming
Logic Programming Languages Session 13 Course : T Programming Language Concept Year : February 2011.
CS 363 Comparative Programming Languages Logic Programming Languages.
Ch. 13 Ch. 131 jcmt CSE 3302 Programming Languages CSE3302 Programming Languages (notes?) Dr. Carter Tiernan.
© Kenneth C. Louden, Chapter 12 - Logic Programming Programming Languages: Principles and Practice, 2nd Ed. Kenneth C. Louden.
Programming Languages Third Edition Chapter 4 Logic Programming.
CS 330 Programming Languages 11 / 25 / 2008 Instructor: Michael Eckmann.
CS 152: Programming Language Paradigms March 5 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak
Programming Language Concepts Lecture 17 Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Logic Programming.
Logic Programming Tarik Booker. What will we cover?  Introduction  Definitions  Predicate Calculus  Prolog  Applications.
ISBN Chapter 16 Logic Programming Languages.
Dr. Muhammed Al-Mulhem ICS An Introduction to Prolog.
Artificial Intelligence CIS 342 The College of Saint Rose David Goldschmidt, Ph.D.
For Friday No reading Prolog Handout 2. Homework.
Prolog 3 Tests and Backtracking 1. Arithmetic Operators Operators for arithmetic and value comparisons are built-in to Prolog = always accessible / don’t.
07/10/04 AIPP Lecture 5: List Processing1 List Processing Artificial Intelligence Programming in Prolog Lecturer: Tim Smith Lecture 5 07/10/04.
Introduction to Prolog Asst. Prof. Dr. Senem Kumova Metin Revised lecture notes of “Concepts of Programmig Languages, Robert W. Sebesta, Ch. 16”
1-1 An Introduction to Prolog Sept Prolog statements Like other programming languages, Prolog consists of collection of statements. Prolog.
CS 330 Programming Languages 12 / 06 / 2007 Instructor: Michael Eckmann.
Section 16.5, 16.6 plus other references
Logic Programming Languages
By P. S. Suryateja Asst. Professor, CSE Vishnu Institute of Technology
Prolog a declarative language
Logic Programming Languages
CS 3304 Comparative Languages
For Friday No reading Prolog handout 3 Chapter 9, exercises 9-11.
For Wednesday Read “lectures” 7-10 of Learn Prolog Now:
Logic Programming Languages
Prolog a declarative language
Prolog a declarative language
Prolog a declarative language
Logic Programming Language
CSE 3302 Programming Languages
Logic Programming Languages
Logic Programming & Prolog
UNIT-V Logic Programming Languages & Functional Programming Languages
CSE 452: Programming Languages
Presentation transcript:

CSE 452: Programming Languages Logical Programming Languages Part 2

2 Organization of Programming Languages-Cheng (Fall 2004) Prolog Statements u Prolog statements consist of facts, rules, and queries. u Example of facts (written as headless Horn clauses) male(tony). male(greg). female(nikki). female(karen). u Example of rules (written as headed Horn clauses) sister(X,Y) :- parent(Z,X), parent(Z,Y), female(X), X \= Y. u Facts and Rules are stored in a file with extension.pl

3 Organization of Programming Languages-Cheng (Fall 2004) Loading the Knowledge Base u Suppose the facts and rules are stored in a file called c:\classes\prolog\gender.pl u To load the file: | ?- ['C:/classes/prolog/gender']. or | ?- consult('C:/classes/prolog/gender').

4 Organization of Programming Languages-Cheng (Fall 2004) Prolog Statements u Goal/Query statements Also in the form of headless Horn clauses | ?- male(tony). yes | ?- female(X). X = nikki ? ; X = karen yes

5 Organization of Programming Languages-Cheng (Fall 2004) Query/Goal Statement u A query can just be an assertion of a fact Prolog will try to establish whether that fact can be shown to be true –1 u Alternatively, a query can contain variables Prolog will try to find values for the variables that make the query true Variables are written with initial uppercase letters. Prolog will print the variable values one by one. After each one, type  “;” to see the next value (solution),  “RETURN” to accept the last value (solution), or  “a” to print all values

6 Organization of Programming Languages-Cheng (Fall 2004) Inference Process u Given a goal/query, how does Prolog match the goal against the propositions in the database? u In general, there are two approaches: Forward chaining (bottom-up resolution)  System begin with the facts and rules of the database and attempt to find a sequence of matches that lead to the goal Backward chaining (top-down resolution)  System begin with the goal and attempts to find a sequence of matching propositions that lead to some set of original facts in the database u Prolog implementation uses backward chaining

7 Organization of Programming Languages-Cheng (Fall 2004) Inference Process (Example) u Example: Given the following facts and rules: father(bob). man(X) :- father(X). Given the following goal/query: man(bob). u Forward chaining: Start with the first proposition: father(bob). Goal is inferred by matching father(bob) to right side of the second rule  This leads to the goal man(bob).

8 Organization of Programming Languages-Cheng (Fall 2004) Inference Process (Example) u Example: Given the following facts and rules: father(bob). man(X) :- father(X). Given the following goal/query: man(bob). u Backward chaining: Start with the goal: man(bob). Match the goal to the left-hand side of second proposition  This would instantiate X to bob Match the right side of the second proposition (father(bob)) to the first proposition

9 Organization of Programming Languages-Cheng (Fall 2004) Inferencing Process of Prolog u What if there are more than one rule that matches a proposition? Which rule should be used next? father(bob). man(X) :- married(Y,Z), son(X,Y). man(X) :- father(X, Y). man(X) :- brother(X, Y). u Solution Search Approaches Depth-first  finds a complete sequence of propositions-a proof-for the first subgoal before working on the others Breadth-first  works on all subgoals of a given goal in parallel Backtracking  when the system finds a subgoal it cannot prove, it reconsiders the previous one to attempt to find an alternative solution and then continue the search- multiple solutions to a subgoal result from different instantiations of its variables

10 Organization of Programming Languages-Cheng (Fall 2004) Simple Arithmetic u Prolog supports integer variables and integer arithmetic + (7, 5). produces an answer equals to 12 A is 5. How about this statement: Sum is Sum + Number  Since Sum is not instantiated, the reference to it on the RHS is undefined and the clause fails

11 Organization of Programming Languages-Cheng (Fall 2004) Simple Arithmetic Continued u Given the following facts: speed( ford, 100). speed(chevy, 105). speed(dodge, 95). time(ford, 20). time(chevy, 21). time(dodge, 24). u We can calculate distance with this rule: distance(X,Y) :- speed(X, Speed), time(X,Time), Y is Speed * Time u Query: | ?- distance(chevy, Chevy_Distance). will instantiate Chevy_Distance to 105*21 = 2205

12 Organization of Programming Languages-Cheng (Fall 2004) Tracing the Process u Prolog has a built-in structure named trace that displays instantiations of values to variables at each step during the attempt to satisfy a given goal trace is used to understand and debug Prolog pgms u Tracing model describes Prolog execution in terms of 4 events  Call, which occurs at the beginning of an attempt to satisfy a goal  Exit, which occurs when a goal has been satisfied  Redo, which occurs when backtrack causes an attempt to resatisfy a goal  Fail, which occurs when a goal fails u In Gnu prolog: trace % This will switch on the trace mode Notrace % This will switch off the trace mode

13 Organization of Programming Languages-Cheng (Fall 2004) Tracing the Process u Consider the following example database and goal: likes(jake, chocolate). likes(jake, apricots). likes(darcie, licorice). likes(darcie, apricots). ?-likes(jake,X), likes(darcie,X). % This is the goal/query u Trace Process: (1)Call:likes(jakes, _0)? (1)Exit: likes(jakes, chocolate) (2)Call:likes(darcie, chocolate)? (2)Fail:likes(darcie,chocolate) (1)Redo:likes(jake, _0)? (1)Exit:likes(jake, apricots) (2)Call:likes(darcie, apricots)? (2)Exit:likes(darcie, apricots) X = apricots

14 Organization of Programming Languages-Cheng (Fall 2004) List Structures u Prolog uses the syntax of ML and Haskell to specify lists List elements are separated by commas, and the entire list is delimited by square brackets Example: [apple, prune, grape, kumquat] Example: [ ] means empty list u Like Haskell, Prolog uses the special notation  [ X | Y ] to indicate head X and tail Y  head and tail correspond CAR and CDR in LISP

15 Organization of Programming Languages-Cheng (Fall 2004) List Structures u Example: Consider the following facts and rules: newlist([apple,apricot,pear,banana ]). first(X) :- newlist([X|Y]).  In query mode, first(X). statement instantiates X with apple

16 Organization of Programming Languages-Cheng (Fall 2004) List Structures u Other Examples: newlist([X|Y]  This will instantiate X to apple and Y to [apricot,pear,banana] newlist([apple,appricot|X]).  This will instantiate X to [pear,banana]

17 Organization of Programming Languages-Cheng (Fall 2004) List Structures u Append: | ?- append(([bob, jo], [jake, darcie], X). produces the output X = [bob, jo, jake, darcie] u Reverse: | ?- reverse([bob, jo, jake, darcie], X). produces the output X = [darcie, jake, jo, bob] u member: | ?- member( bob, [darcie, jo, jim, bob, alice]). The system responds with the answer Yes

18 Organization of Programming Languages-Cheng (Fall 2004) List Structures u Writing your own append function: | ?- myappend([],[banana,eggs,milk],List). List = [banana,eggs,milk] | ?- myappend([],List,[banana,eggs,milk]). List = [banana,eggs,milk] | ?- myappend(List1,List2,[banana,eggs,milk]). List1 = [ ] List2 = [banana,eggs,milk] ? ; List1 = [banana] List2 = [eggs,milk] ? ; List1 = [banana,eggs] List2 = [milk] ? ; List1 = [banana,eggs,milk] List2 = [ ] ? ;

19 Organization of Programming Languages-Cheng (Fall 2004) List Structures u Writing your own append function: myappend([ ],List,List). myappend([Head1|Tail1],List2,[Head1|List3]) :- myappend(Tail1,List2,List3). The first proposition specifies when the empty list is appended to any other list, that other list is the result The second proposition specifies the characteristics of the new list  Appending the list [Head1|Tail1] to List2 produces the list [Head1|List3] only if List3 is obtained by appending Tail1 and List2

20 Organization of Programming Languages-Cheng (Fall 2004) List Structures u Write your own reverse function myreverse([],[]). myreverse([Head|Tail],List) :- myreverse(Tail,Result), append(Result,[Head],List).

21 Organization of Programming Languages-Cheng (Fall 2004) List Structures u Write your own member function: mymember(Element,[Element|_]). mymember(Element,[_|List]) :- member(Element,List).

22 Organization of Programming Languages-Cheng (Fall 2004) Size | ?- size([a,b,c], N). N = 3 ? yes u Note: There is a predefined function called length which is the same as size size([ ], 0). size([H | T], N) :- size(T, N1), N is N1+1.

23 Organization of Programming Languages-Cheng (Fall 2004) Towers of Hanoi u The object is to move the disks, one at a time, from the left peg to the right peg. u You are allowed to use the middle peg. u At no stage are you allowed to place a bigger disk on top of a smaller one. left middleright

24 Organization of Programming Languages-Cheng (Fall 2004) Towers of Hanoi Move top disk from left to right left middleright

25 Organization of Programming Languages-Cheng (Fall 2004) Towers of Hanoi Move top disk from left to right Move top disk from left to middle left middleright

26 Organization of Programming Languages-Cheng (Fall 2004) Towers of Hanoi Move top disk from left to right Move top disk from left to middle Move top disk from right to middle left middleright

27 Organization of Programming Languages-Cheng (Fall 2004) Towers of Hanoi Move top disk from left to right Move top disk from left to middle Move top disk from right to middle Move top disk from left to right left middleright

28 Organization of Programming Languages-Cheng (Fall 2004) Towers of Hanoi Move top disk from left to right Move top disk from left to middle Move top disk from right to middle Move top disk from left to right Move top disk from middle to left left middleright

29 Organization of Programming Languages-Cheng (Fall 2004) Towers of Hanoi Move top disk from left to right Move top disk from left to middle Move top disk from right to middle Move top disk from left to right Move top disk from middle to left Move top disk from middle to right left middleright

30 Organization of Programming Languages-Cheng (Fall 2004) Towers of Hanoi Move top disk from left to right Move top disk from left to middle Move top disk from right to middle Move top disk from left to right Move top disk from middle to left Move top disk from middle to right Move top disk from left to right left middleright

31 Organization of Programming Languages-Cheng (Fall 2004) Recursive Solution u Suppose we have N disk on the source pole. u The idea is that if we can move the top N-1 disks onto the middle pole, then we can simply move the largest disk at the bottom onto the destination pole. u By applying the same technique that we use to move the top N-1 disks, we can move the N-1 disks onto the dest pole.

32 Organization of Programming Languages-Cheng (Fall 2004) Recursive Solution Original Step 1 Step 2Step 3

33 Organization of Programming Languages-Cheng (Fall 2004) Solution move(1, X, Y, Z) :- write(’ Move top disk from ’), write(X), write(’ to ’), write(Y), nl. move(N, X, Y, Z) :- N > 1, M is N-1, move(M, X, Z, Y), move(1, X, Y, Z), move(M, Z, Y, X). | ?- move(3, left, right, middle).

34 Organization of Programming Languages-Cheng (Fall 2004) Solution | ?- move(3,left,right,middle). Move top disk from left to right Move top disk from left to middle Move top disk from right to middle Move top disk from left to right Move top disk from middle to left Move top disk from middle to right Move top disk from left to right true ?

35 Organization of Programming Languages-Cheng (Fall 2004) Quick Sort (Exercise) < pp >= p Partition Sort p

36 Organization of Programming Languages-Cheng (Fall 2004) Resolution Order Control u Prolog always matches in the same order: starting at the beginning and at the left end of a given goal u User can affect efficiency of resolution by ordering the propositions to optimize a particular application If certain rules are more likely to succeed than others during an execution, placing those rules first makes the program more efficient

37 Organization of Programming Languages-Cheng (Fall 2004) Control of Backtracking u The cut operator (!) is used to control backtracking u The (!) is actually a goal which always succeeds immediately, but it cannot be re-satisfied through backtracking u For example, the goal A, B, !, C, D. would follow a left to right unification process up to C. If A and B succeed but C fails, we know it is a waste of time to backtrack to resatisfy A or B, so the whole goal fails if we use ! in the place immediately before C

38 Organization of Programming Languages-Cheng (Fall 2004) Problems with Control u The ability to tamper with control flow in a Prolog program is a deficiency because it is detrimental to one of the advantages of logic programming: programs do not specify how to find solutions u Using the ability for flow control, clutters the simplicity of logic programs with details of order control to produce solutions u Frequently used for the sake of efficiency

39 Organization of Programming Languages-Cheng (Fall 2004) Negation u Consider the example parent(bill, jake). parent(bill, shelley). sibling(X,Y) :- parent(M,X), parent(M,Y). ?-sibling(X,Y). Prolog’s result is X = jake Y = jake

40 Organization of Programming Languages-Cheng (Fall 2004) Negation u Consider the example parent(bill, jake). parent(bill, shelley). sibling(X,Y) :- parent(M,X), parent(M,Y), X \=Y. ?-sibling(X,Y). Prolog’s result is X = jake Y = shelley

41 Organization of Programming Languages-Cheng (Fall 2004) Negation not a Logical Not u Negation means resolution cannot satisfy the subgoal u Logical Not cannot be a part of Prolog primarily because of the form of the Horn clause B :- A 1  A 2 ...  A m If all the A propositions are true, it can be concluded that B is true. But regardless of the truth or falseness of any or all of the As, it cannot be concluded that B is false. Prolog can prove that a given goal is true, but it cannot prove that a given goal is false.

42 Organization of Programming Languages-Cheng (Fall 2004) Expert Systems u Expert Systems are designed to emulate human expertise. They consist of a database of facts, an inferencing process, some heuristics about the domain, and some friendly human interface u Prolog provides the facilities of using resolution for query processing, adding facts and rules to provide the learning capability, and using trace to inform the user of the reasoning behind a given result