CSC 3315 Programming Paradigms Prolog Language Hamid Harroud School of Science and Engineering, Akhawayn University http://www.aui.ma/~H.Harroud/csc3315/ CSC3315 (Spring 2009) 1 Logic Programming Languages 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 1-2 Introduction Logic programming language or declarative programming language Express programs in a form of symbolic logic Use a logical inferencing process to produce results Declarative rather that procedural: Only specification of results are stated (not detailed procedures for producing them) 1-3 Proposition A logical statement that may or may not be true Consists of objects and relationships of objects to each other 1-4 Symbolic Logic Logic which can be used for the basic needs of formal logic: Express propositions Express relationships between propositions Describe how new propositions can be inferred from other propositions Particular form of symbolic logic used for logic programming called predicate calculus 1-5 Object Representation Objects in propositions are represented by simple terms: either constants or variables Constant: a symbol that represents an object Variable: a symbol that can represent different objects at different times Different from variables in imperative languages 1-6 Compound Terms Atomic propositions consist of compound terms Compound term: one element of a mathematical relation, written like a mathematical function Mathematical function is a mapping Can be written as a table 1-7 Parts of a Compound Term Compound term composed of two parts Functor: function symbol that names the relationship Ordered list of parameters (tuple) Examples: student(john) like(nick, windows) like(jim, linux) 1-8 Forms of a Proposition Propositions can be stated in two forms: Fact: proposition is assumed to be true Query: truth of proposition is to be determined Compound proposition: Have two or more atomic propositions Propositions are connected by operators 1-9 Logical Operators Name Symbol Example Meaning negation a not a conjunction ab a and b disjunction ab a or b equivalence ab a is equivalent to b implication ab ab a implies b b implies a 1-10 Quantifiers Name Example Meaning universal X.P For all X, P is true existential X.P There exists a value of X such that P is true 1-11 Clausal Form Too many ways to state the same thing Use a standard form for propositions Clausal form: B1 B2 … Bn A1 A2 … Am means if all the As are true, then at least one B is true Antecedent: right side Consequent: left side 1-12 Predicate Calculus and Proving Theorems A use of propositions is to discover new theorems that can be inferred from known axioms and theorems Resolution: an inference principle that allows inferred propositions to be computed from given propositions 1-13 Resolution Unification: finding values for variables in propositions that allows matching process to succeed Instantiation: assigning temporary values to variables to allow unification to succeed After instantiating a variable with a value, if matching fails, may need to backtrack and instantiate with a different value 1-14 Proof by Contradiction Hypotheses: a set of pertinent propositions Goal: negation of theorem stated as a proposition Theorem is proved by finding an inconsistency 1-15 Theorem Proving Basis for logic programming When propositions used for resolution, only restricted form can be used Horn clause - can have only two forms Headed: single atomic proposition on left side Headless: empty left side (used to state facts) Most propositions can be stated as Horn clauses 1-16 Overview of Logic Programming Declarative semantics There is a simple way to determine the meaning of each statement Simpler than the semantics of imperative languages Programming is nonprocedural Programs do not state now a result is to be computed, but rather the form of the result 1-17 Example: Sorting a List Describe the characteristics of a sorted list, not the process of rearranging a list sort(old_list, new_list) permute (old_list, new_list) sorted (new_list) sorted (list) j such that 1 j < n, list(j) list (j+1) 1-18 The Origins of Prolog University of Aix-Marseille Natural language processing University of Edinburgh Automated theorem proving 1-19 Terms Edinburgh Syntax Term: a constant, variable, or structure Constant: an atom or an integer Atom: symbolic value of Prolog Atom consists of either: a string of letters, digits, and underscores beginning with a lowercase letter a string of printable ASCII characters delimited by apostrophes 1-20 Terms: Variables and Structures Variable: any string of letters, digits, and underscores beginning with an uppercase letter Instantiation: binding of a variable to a value Lasts only as long as it takes to satisfy one complete goal Structure: represents atomic proposition functor(parameter list) 1-21 Fact Statements Used for the hypotheses Headless Horn clauses female(shelley). male(bill). father(bill, jake). 1-22 Rule Statements Used for the hypotheses Headed Horn clause Right side: antecedent (if part) Left side: consequent (then part) May be single term or conjunction Must be single term Conjunction: multiple terms separated by logical AND operations (implied) 1-23 Example Rules ancestor(mary,shelley):- mother(mary,shelley). Can use variables (universal objects) to generalize meaning: parent(X,Y):- mother(X,Y). parent(X,Y):- father(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). 1-24 Goal Statements For theorem proving, theorem is in form of proposition that we want system to prove or disprove – goal statement Same format as headless Horn man(fred) Conjunctive propositions and propositions with variables also legal goals father(X,mike) 1-25 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 called matching, satisfying, or resolution 1-26 Approaches Bottom-up resolution, forward chaining Top-down resolution, backward 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 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-27 Subgoal Strategies 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 1-28 Backtracking 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 1-29 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 / 17 + C Not the same as an assignment statement! 1-30 Example 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. 1-31 List Structures Other basic data structure (besides atomic propositions we have already seen): list List is a sequence of any number of elements Elements can be atoms, atomic propositions, or other terms (including other lists) [apple, prune, grape, kiwi] [] (empty list) [X | Y] (head X and tail Y) 1-32 Append Example append([], List, List). append([Head | List_1], List_2, [Head | List_3]) :append (List_1, List_2, List_3). 1-33 Reverse Example reverse([], []). reverse([Head | Tail], List) :reverse (Tail, Result), append (Result, [Head], List). 1-34 Example: mylength mylength([],0). mylength([_|Tail], Len) :mylength(Tail, TailLen), Len is TailLen + 1. ?- mylength([a,b,c],X). X = 3 Yes ?- mylength(X,3). X = [_G266, _G269, _G272] Yes Counterexample: mylength mylength([],0). mylength([_|Tail], Len) :mylength(Tail, TailLen), Len = TailLen + 1. ?- mylength([1,2,3,4,5],X). X = 0+1+1+1+1+1 Yes Example: sum sum([],0). sum([Head|Tail],X) :sum(Tail,TailSum), X is Head + TailSum. ?- sum([1,2,3],X). X = 6 Yes ?- sum([1,2.5,3],X). X = 6.5 Yes Example: gcd gcd(X,Y,Z) :X =:= Y, Z is X. gcd(X,Y,Denom) :X < Y, NewY is Y - X, gcd(X,NewY,Denom). gcd(X,Y,Denom) :X > Y, NewX is X - Y, gcd(NewX,Y,Denom). Note: not just gcd(X,X,X) The gcd Predicate At Work ?- gcd(5,5,X). X = 5 Yes ?- gcd(12,21,X). X = 3 Yes ?- gcd(91,105,X). X = 7 Yes ?- gcd(91,X,7). ERROR: Arguments are not sufficiently instantiated Example: factorial factorial(X,1) :X =:= 1. factorial(X,Fact) :X > 1, NewX is X - 1, factorial(NewX,NF), Fact is X * NF. ?- factorial(5,X). X = 120 Yes ?- factorial(20,X). X = 2.4329e+018 Yes ?- factorial(-2,X). No Problem Space Search Prolog’s strength is (obviously) not numeric computation The kinds of problems it does best on are those that involve problem space search You give a logical definition of the solution Then let Prolog find it The Knapsack Problem You are packing for a camping trip Your pantry contains these items: Item Weight in kilograms Calories bread 4 9200 pasta 2 4600 peanut butter 1 6700 baby food 3 6900 Your knapsack holds 4 kg. What choice <= 4 kg. maximizes calories? Greedy Methods Do Not Work Item Weight in kilograms Calories bread 4 9200 pasta 2 4600 peanut butter 1 6700 baby food 3 6900 Most calories first: bread only, 9200 Lightest first: peanut butter + pasta, 11300 (Best choice: peanut butter + baby food, 13600) Search No algorithm for this problem is known that Always gives the best answer, and Takes less than exponential time So brute-force search is used here That’s good, since search is something Prolog does really well Representation We will represent each food item as a term food(N,W,C) Pantry in our example is [food(bread,4,9200), food(pasta,2,4500), food(peanutButter,1,6700), food(babyFood,3,6900)] Same representation for knapsack contents The Knapsack Problem /* weight(L,N) takes a list L of food terms, N is the sum of all the Weights. */ weight([],0). weight([food(_,W,_) | Rest], X) :weight(Rest,RestW), X is W + RestW. /* calories(L,N) takes a list L of food terms, N is the sum of all the Calories. */ calories([],0). calories([food(_,_,C) | Rest], X) :calories(Rest,RestC), X is C + RestC. The Knapsack Problem /* subseq(X,Y) succeeds when list X is the same as list Y, but with zero or more elements omitted. */ subseq([],[]). subseq([Item | RestX], [Item | RestY]) :subseq(RestX,RestY). subseq(X, [_ | RestY]) :subseq(X,RestY). A subsequence of a list is a copy of the list with any number of elements omitted (Knapsacks are subsequences of the pantry) The Knapsack Problem ?- subseq([1,3],[1,2,3,4]). Yes ?- subseq(X,[1,2,3]). X X X X X X X X = = = = = = = = No [1, 2, [1, 2] [1, 3] [1] ; [2, 3] [2] ; [3] ; [] ; 3] ; ; ; ; Note that subseq can do more than just test whether one list is a subsequence of another; it can generate subsequences, which is how we will use it for the knapsack problem. The Knapsack Problem /* knapsackDec(Pantry,Capacity,Goal,Knapsack) takes a list Pantry of food terms, a positive number Capacity, and a positive number Goal. We unify Knapsack with a subsequence of Pantry representing a knapsack with total calories >= goal, subject to the constraint that the total weight is =< Capacity. */ knapsackDec(Pantry,Capacity,Goal,Knapsack) :subseq(Knapsack,Pantry), weight(Knapsack,Weight), Weight =< Capacity, calories(Knapsack,Calories), Calories >= Goal. The Knapsack Problem ?- knapsackDec( | [food(bread,4,9200), | food(pasta,2,4500), | food(peanutButter,1,6700), | food(babyFood,3,6900)], | 4, | 10000, | X). X = [food(pasta, 2, 4500), food(peanutButter, 1, 6700)] Yes This decides whether there is a solution that meets the given calorie goal The 8-Queens Problem Chess background: Played on an 8-by-8 grid Queen can move any number of spaces vertically, horizontally or diagonally Two queens are in check if they are in the same row, column or diagonal, so that one could move to the other’s square The problem: place 8 queens on an empty chess board so that no queen is in check Representation We could represent a queen in column 2, row 5 with the term queen(2,5) But it will be more readable if we use something more compact Since there will be no other pieces, we can just use a term of the form X/Y (We won’t evaluate it as a quotient) Example 8 Q 7 Q 6 Q 5 Q Q 4 Q 3 Q 2 1 Q Our 8-queens solution 1 [8/4, 7/2, 6/7, 5/3, 4/6, 3/8, 2/5, 1/1] 2 3 4 5 6 7 8 The 8-Queens Problem /* nocheck(X/Y,L) takes a queen X/Y and a list of queens. We succeed if and only if the X/Y queen holds none of the others in check. */ nocheck(_, []). nocheck(X/Y, [X1/Y1 | Rest]) :X =\= X1, Y =\= Y1, abs(Y1-Y) =\= abs(X1-X), nocheck(X/Y, Rest). The 8-Queens Problem /* legal(L) succeeds if L is a legal placement of queens: all coordinates in range and no queen in check. */ legal([]). legal([X/Y | Rest]) :legal(Rest), member(X,[1,2,3,4,5,6,7,8]), member(Y,[1,2,3,4,5,6,7,8]), nocheck(X/Y, Rest). Adequate This is already enough to solve the problem: the query legal(X) will find all legal configurations: ?- legal(X). X = [] ; X = [1/1] ; X = [1/2] ; X = [1/3] 8-Queens Solution Of course that will take too long: it finds all 64 solutions with one queen, then starts on those with two, and so on To make it concentrate right away on eight queens, we can give a different query: ?- X = [_,_,_,_,_,_,_,_], legal(X). X = [8/4, 7/2, 6/7, 5/3, 4/6, 3/8, 2/5, 1/1] Yes Room For Improvement Slow Finds trivial permutations after the first: ?- X = [_,_,_,_,_,_,_,_], legal(X). X = [8/4, 7/2, 6/7, 5/3, 4/6, 3/8, 2/5, 1/1] ; X = [7/2, 8/4, 6/7, 5/3, 4/6, 3/8, 2/5, 1/1] ; X = [8/4, 6/7, 7/2, 5/3, 4/6, 3/8, 2/5, 1/1] ; X = [6/7, 8/4, 7/2, 5/3, 4/6, 3/8, 2/5, 1/1] An Improvement Clearly every solution has 1 queen in each column So every solution can be written in a fixed order, like this: X=[1/_,2/_,3/_,4/_,5/_,6/_,7/_,8/_] Starting with a goal term of that form will restrict the search (speeding it up) and avoid those trivial permutations The 8-Queens Problem /* eightqueens(X) succeeds if X is a legal placement of eight queens, listed in order of their X coordinates. */ eightqueens(X) :X = [1/_,2/_,3/_,4/_,5/_,6/_,7/_,8/_], legal(X). The 8-Queens Problem nocheck(_, []). nocheck(X/Y, [X1/Y1 | Rest]) :% X =\= X1, assume the X's are distinct Y =\= Y1, abs(Y1-Y) =\= abs(X1-X), nocheck(X/Y, Rest). legal([]). legal([X/Y | Rest]) :legal(Rest), % member(X,[1,2,3,4,5,6,7,8]), assume X in range member(Y,[1,2,3,4,5,6,7,8]), nocheck(X/Y, Rest). Since all X-coordinates are already known to be in range and distinct, these can be optimized a little Improved 8-Queens Solution Now much faster Does not bother with permutations ?- eightqueens(X). X = [1/4, 2/2, 3/7, 4/3, 5/6, 6/8, 7/5, 8/1] ; X = [1/5, 2/2, 3/4, 4/7, 5/3, 6/8, 7/6, 8/1] Cut & Negation Cut (Minimum): mini1(X, Y, X) :- X < Y. mini1(X, Y, Y) :- X >= Y. mini2(X, Y, X) :- X < Y, !. mini2(_, Y, Y). mini3(X, Y, Z) :- X < Y, !, Z = X. mini3(X, Y, Y). Negation: not(P):- P, ! ,fail. not(P). %sinon Applications of Logic Programming Relational database management systems Expert systems Natural language processing 1-64 Summary Symbolic logic provides basis for logic programming Logic programs should be nonprocedural Prolog statements are facts, rules, or goals Resolution is the primary activity of a Prolog interpreter Although there are a number of drawbacks with the current state of logic programming it has been used in a number of areas 1-65