460 likes | 699 Views
An Introduction to Prolog. Sept. 2008. 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. Facts.
E N D
An Introduction to Prolog Sept. 2008
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
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.
fortran algol60 simula67 cpl bcpl smalltalk80 cplusplus c Facts (contd.) 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).
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
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).
fortran algol60 simula67 cpl bcpl smalltalk80 cplusplus c Rules (contd.) Example Rules: path(X,Y) :- link(X,Z) , link(Z,Y). 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).
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.
Goals (contd.) • 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*/
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.
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
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.
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
Inferencing Process of Prolog (Contd.) • 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.) • 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.
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.
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.
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.
List Structures (Contd.) • 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.) • 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]])
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).
Example 2 • Reversing a list • reverse([ ], [ ]). • reverse([Head|Tail], List) :- reverse(Tail,Result), append(Result, [Head],List).
Example 3 • 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 4 Definition of sum function: sum([],0). sum([H|T],N):-sum(T,M), N is H+M.
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.
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 • xy for integers x and y.
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).
Deficiencies of Prolog • Resolution order control • The closed-world assumption • The negation problem • Intrinsic limitations
Deficiencies of Prolog (Contd.) 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.) Resolution Order Control (Cont.) • The cut operator ! • Can eliminate backtracking • Is useful when a proposition can only be satisfied once • Form isa,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.) 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.) 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.) 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.
Applications of Logic Programming • Relational database management systems • Expert systems • Natural language processing • Education
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
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