1-1 An Introduction to Prolog Sept. 2008. 1-2 Prolog statements Like other programming languages, Prolog consists of collection of statements. Prolog.

Slides:



Advertisements
Similar presentations
First Order Logic Logic is a mathematical attempt to formalize the way we think. First-order predicate calculus was created in an attempt to mechanize.
Advertisements

Chapter 11 :: Logic Languages
1 Introduction to Prolog References: – – Bratko, I., Prolog Programming.
Inference and Reasoning. Basic Idea Given a set of statements, does a new statement logically follow from this. For example If an animal has wings and.
Logic.
CS 330 Programming Languages 12 / 02 / 2008 Instructor: Michael Eckmann.
Chapter 12 - Logic Programming
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/
Programming Languages Third Edition
Constraint Logic Programming Ryan Kinworthy. Overview Introduction Logic Programming LP as a constraint programming language Constraint Logic Programming.
Logic Programming Languages. Objective To introduce the concepts of logic programming and logic programming languages To introduce a brief description.
CSE 452: Programming Languages Logical Programming Languages Part 2.
ISBN Chapter 16 Logic Programming Languages.
Introduction to prolog (1). Introduction to Prolog 2 Overviews Interacting with Prolog Terms Existential Queries Universal Facts and Rules Negation as.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 15 Logic Programming Q: How many legs does.
CS 330 Programming Languages 12 / 05 / 2006 Instructor: Michael Eckmann.
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
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.
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.
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.
CHAPTER 15 & 16 Functional & Logic Programming Languages.
1 Prolog and Logic Languages Aaron Bloomfield CS 415 Fall 2005.
Logic Programming CSC 358/ Outline Pattern matching Unification Logic programming.
By: Cory Canter CSC 415 Programming Languages. History  Created by Alain Colmerauer, Phillipe Roussel and Robert Kowalski in 1971  Started as a natural.
Dr. Muhammed Al-Mulhem ICS An Introduction to Logical Programming.
Logic Programming Languages Session 13 Course : T Programming Language Concept Year : February 2011.
CS 363 Comparative Programming Languages Logic Programming Languages.
Automated Reasoning Early AI explored how to automated several reasoning tasks – these were solved by what we might call weak problem solving methods as.
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.
© Copyright 2008 STI INNSBRUCK Intelligent Systems Propositional Logic.
CS 330 Programming Languages 11 / 25 / 2008 Instructor: Michael Eckmann.
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.
1-1 An Introduction to Logical Programming Sept
In The Name Of Allah Lab 03 1Tahani Aldweesh. objectives Searching for the solution’s. Declaration. Query. Comments. Prolog Concepts. Unification. Disjunction.
Artificial Intelligence CIS 342 The College of Saint Rose David Goldschmidt, Ph.D.
For Friday No reading Prolog Handout 2. Homework.
Introduction to Prolog Asst. Prof. Dr. Senem Kumova Metin Revised lecture notes of “Concepts of Programmig Languages, Robert W. Sebesta, Ch. 16”
Logical Agents Chapter 7. Outline Knowledge-based agents Propositional (Boolean) logic Equivalence, validity, satisfiability Inference rules and theorem.
Prolog Fundamentals. 2 Review Last Lecture A Prolog program consists of a database of facts and rules, and queries (questions). –Fact:.... –Rule:... :-....
Copyright 1999Paul F. Reynolds, Jr. Foundations of Logic Programming.
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
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
Logic Programming Languages
Prolog a declarative language
Prolog a declarative language
Prolog a declarative language
Logic Programming Language
Logic Programming & Prolog
This Lecture Substitution model
UNIT-V Logic Programming Languages & Functional Programming Languages
Presentation transcript:

1-1 An Introduction to Prolog Sept. 2008

1-2 Prolog statements Like other programming languages, Prolog consists of collection of statements. Prolog has two basic statement forms: –Headless Horn clause – called facts –Headed Horn clause – called rules

1-3 Facts Represent statements that are always true. The parameters are usually (but not always) constants Examples: –female(mary). –male(bill) –male(jake) –father( bill, jake). –mother( mary, jake). These simple structure state certain facts about jake, bill and mary. Note that these Prolog facts have no intrinsic semantics. They mean whatever the programmer wants them to mean. For example father( bill, jake). Could mean: –Bill and jake have the same father –Jake is the father of bill The most common meaning is that bill is the fatehr of jake.

1-4 Facts (contd.) fortran algol60 cpl bcpl c simula67 cplusplus smalltalk80 Example Facts: link(fortran,algol60). link(c,cplusplus). link(algol60,cpl). link(algol60,simula67). link(cpl,bcpl). link(simula67,cplusplus). link(bcpl,c). link(simula67,smalltalk8).

1-5 Rules This is the other basic form of Prolog statement. Used to construct the database corresponds of facts. It is a headed Horn clause Use :- instead of  and a comma instead of a  Right side: antecedent (if part) –May be single term or conjunction Left side: consequent (then part) –Must be single term

1-6 Rules (contd.) parent(kim,kathy):- mother(kim,kathy). Can use variables (universal objects) to generalize meaning: parent(X,Y):- mother(X,Y). sibling(X,Y):- mother(M,X), mother(M,Y), father(F,X), father(F,Y).

1-7 Rules (contd.) Example Rules: path(X,Y) :- link(X,Z), link(Z,Y). fortran algol60 cpl bcpl c simula67 cplusplus smalltalk80 Example Facts: link(fortran,algol60). link(c,cplusplus). link(algol60,cpl). link(algol60,simula67). link(cpl,bcpl). link(simula67,cplusplus). link(bcpl,c). link(simula67,smalltalk8).

1-8 Goals Facts and rules are used to describe both known facts and rules that describe logical relationships among facts. These statements are the basis for the theorem proving model. The theorem is in the form of a proposition that we want the system to either prove or disprove. In Prolog, these propositions are called goals. A series of one or more propositions, separated by commas Should be thought of as a query If the parameters of the goal are all constants, then the answer to the query should be “Yes” or “No” If the parameters of the goal contain variable(s), then the answer to the query should be all the values for those variables which satisfy the query, or “No” if no value satisfies it.

1-9 Example: –link(algo60,L), link(L,M). /* “Are there some values for L and M such that algo60 is linked to L and L is linked to M?” */ ============================================= –male(ahmad). /* Answer should be Yes/No */ –father(X,ali). /* Answer should be X = “ ??” or No */ –father(ali,naser). /* Answer should be Yes/No */ –father(bill,X), mother(mary,X). /* Answer should be X = “??? or NO*/ Goals (contd.)

1-10 Prolog Programs Are a series of facts and/or rules. Can be placed in any order, due to the nonprocedural nature of logic-based languages Are “executed” in a Prolog environment using goal statements.

1-11 Inferencing Process of Prolog If a goal is a compound proposition, each of the facts is a subgoal. To prove a goal is true, the inferencing process must find a chain of inference rules and/or facts in the database that connect the goal to one or more facts in the database. For example, if Q is a goal, then either Q must be found as a fact in the database or the inferencing process must find a fact P1 and a sequence of propositions P2, P3, …Pn such that P2 :- P1. P3 :- P2. … Q :- Pn. Process of proving a subgoal is called matching, satisfying, or resolution

1-12 Example Consider this goal man(bob) This goal is compared with the facts and rules in the database. If the database includes the fact man(bob) The proof is trivial—Yes. If, however, the database contains the following fact and rule. father(bob) man(X) :- father(X) Prolog should find these two statements and use them to infere truth of the goal.

1-13 Trace Example

1-14 Inferencing Process of Prolog (Contd.) Bottom-up resolution, forward chaining –Begin with facts and rules of database and attempt to find sequence that leads to goal –works well with a large set of possibly correct answers Top-down resolution, backward chaining –begin with goal and attempt to find sequence that leads to set of facts in database –works well with a small set of possibly correct answers Prolog implementations use backward chaining

1-15 When goal has more than one subgoal, can 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 –Can be done with fewer computer resources Inferencing Process of Prolog (Contd.)

1-16 With a goal with multiple subgoals, if fail to show truth of one of subgoals, reconsider previous subgoal to find an alternative solution: backtracking. Begin search where previous search left off. Can take lots of time and space because may find all possible proofs to every subgoal. Inferencing Process of Prolog (Contd.)

1-17 Simple Arithmetic Prolog supports integer variables and integer arithmetic is operator: takes an arithmetic expression as right operand and variable as left operand A is B / 10 + C. Not the same as an assignment statement! Should not be done with parameters Either both sides must have all variables instantiated (in which case is acts as a relational =) or just the lefthand side is not instantiated (which means the lhs receives a value) Therefore, the following is never appropriate: –Sum is Sum + Number.

1-18 Arithmetic Example

1-19 Arithmetic Example (Contd.)

1-20 Recursion Is the only way to do iteration in Prolog Is usually accomplished with at least one fact and one rule Example: Consider the following mathematical definition of factorial: –0! = 1 –n! = (n-1)! * n  n > 0 Here is the equivalent Prolog statements: –fact(0,1). –fact(N,NFact) :- N > 0, N1 is N-1, fact(N1,N1Fact), NFact is N1Fact * N.

1-21 List Structures The value of a list consists of zero or more elements, separated by commas and enclosed in square brackets. Example: [apple, prune, grape, kumquat] Each element can be an atom or a list A variable such a L can be used to represent an entire list in a statement. The expression [E] in a statement denotes a one-element list. The expression [ ] in a statement denotes an empty list.

1-22 The expression [X | Y] in a statement denotes a list with one or more elements where the first element is the head X and the rest of the list (which may be empty) is the tail Y. –This is how recursion can be used to traverse each element of a list. –X is called the “car” and Y is called the “cdr”. (These terms are from Lisp.) –For example, in [apple, prune, grape, kumquat], apple is the car, and [prune, grape, kumquat] is the cdr. List Structures (Contd.)

1-23 List Structures (Contd.) A list can be created with simple proposition. new_list ([apple, prune, grape]) This does the kind of thing that the proposition male(ahmad) does. We could have a second proposition like new_list ([ apricot, peach, pear]) In goal mode, the list can be dismantled into head and tail. new_list ([ Head, Tail]) Then Head is instantiated to apricot, and Tail to [peach, pear] The | can specify a list construction or a list dismanteling. Note that the following are equivalent: new_list ([ apricot, peach, pear | [ ]]) new_list ([ apricot, peach | [pear]]) new_list ([ apricot | [peach, pear]])

1-24 Example 1 Appending two lists together –append([ ],List,List). –append([Head|List_1],List_2,[Head|List_3]) :- append(List_1,List_2, List_3). The first one specifies that when the empty list is appended to any other list, that list is the result. The second one specifies several characteristics of the new list. The left-side states that the fist element of the new list is the same as the first element of the first given list, because they are both named Head. The right-side specifies that the tail of the first given list (List_1) has the second given list (List_2) appended to it to form the tail (List_3).

1-25 Example 1 (Contd.)

1-26 Reversing a list –reverse([ ], [ ]). –reverse([Head|Tail], List) :- reverse(Tail,Result), append(Result, [Head],List). Example 2

1-27 Example 2 (Cont.)

1-28 Seeing if a list has a particular member –member(Element,[Element| _ ]). –member(Element,[ _|List] :- member(Element,List). The _ is an “anonymous” variable; i.e., we don’t care what the value is, although a value does need to be there. Example 3

1-29 Example 3 (Contd.)

1-30 Example 4 Definition of sum function: sum([],0). sum([H|T],N):-sum(T,M), N is H+M.

1-31 Example 6 Definition of findOccurrences function: findOccurrences(X,[],0). findOccurrences(X,[X|T],N):- findOccurrences(X,T,Z), N is Z+1. findOccurrences(X,[_|T],N):- findOccurrences(X,T,Z), N is Z.

1-32 Useful Exercises Write a Prolog functor that interleaves two lists. For example given the query: ?- interleave([1,2,3,4,5],[6,7,8,9,10],X). It should return X = [1,6,2,7,3,8,4,9,5,10] Write a Prolog functor that succeeds if its list input consists of palindrome values. For example given the query: ?- palindrome([1,2,3,4,5,4,3,2,1]). It should return Yes. Write functors to compute: – the Fibonacci function – x y for integers x and y.

1-33 Example 5 Definition of diffList function: diffList([], List, []). diffList([H|L1], L2, L3) :- not(member(H,L2)), diffList (L1, L2, L4), append([H],L4,L3). diffList([_|L1], L2, L3) :- diffList (L1, L2, L3).

1-34 Deficiencies of Prolog Resolution order control The closed-world assumption The negation problem Intrinsic limitations

1-35 Resolution Order Control Depth-first search method can cause infinite recursion –Example: ancestor(X,X). ancestor(X,Y) :- ancestor(Z,Y), parent(X,Z). –Keeps trying to satisfy the second rule –Can be solved by reversing the two propositions on the right, but that is against the basic nonprocedural philosophy of Prolog Deficiencies of Prolog (Contd.)

1-36 Resolution Order Control (Cont.) The cut operator ! –Can eliminate backtracking –Is useful when a proposition can only be satisfied once –Form is a,b,!,c,d If c is not satisfied, the statement cannot go back and find another possible value for b –Example: member(Element, [Element | _ ]) :- ! member(Element, [ _ | List]) :- member(Element,List). The change in the first statement assumes that the list consists of unique members. –The cut operator also is contrary to the Prolog philosophy of nonprocedural programming Deficiencies of Prolog (Contd.)

1-37 Close World Assumption If Prolog has insufficient data to answer a question, the answer is “no”, just as it would be if it had sufficient data to answer “no”. Deficiencies of Prolog (Contd.)

1-38 The Negation Problem Consider the following statement: –sibling(X,Y) :- parent(M,X), parent(M,Y). –Nothing keeps a person from being their own sibling! Can be solved with a not proposition: –sibling(X,Y) :- parent(M,X), parent(M,Y),not(X = Y). However, the not proposition is not a not operator (double negation is not allowed), which causes some limitations Deficiencies of Prolog (Contd.)

1-39 Intrinsic Limitations Prolog is often not efficient Example: sorted([ ]). sorted([x]. sorted([x, y | list]) :- x <= y, sorted([y | list]). –All permutations of list must be tried until the right one is found. Deficiencies of Prolog (Contd.)

1-40 Applications of Logic Programming Relational database management systems Expert systems Natural language processing Education

1-41 Conclusions Advantages: –Prolog programs based on logic, so likely to be more logically organized and written –Processing is naturally parallel, so Prolog interpreters can take advantage of multi-processor machines –Programs are concise, so development time is decreased – good for prototyping

1-42 Summary Predicate calculus provides a formal means for logical expressions (I.e. those that evaluate to true or false) Horn Clauses provide a particular structure which can be used for most logical expressions Declarative semantics allows both for the focus of problem solving to be on “what” rather than “how” and for nonprocedural programming Prolog uses declarative semantics There are some deficiencies in Prolog, some of which are inherent to declarative semantics