42
Dr. Muhammed Al-Mulhe Dr. Muhammed Al-Mulhe m ICS535-101 ICS535-101 1 An Introduction to An Introduction to Prolog Prolog

An Introduction to Prolog

  • Upload
    pegeen

  • View
    48

  • Download
    0

Embed Size (px)

DESCRIPTION

An Introduction to Prolog. 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. - PowerPoint PPT Presentation

Citation preview

Page 1: An Introduction to Prolog

Dr. Muhammed Al-MulhemDr. Muhammed Al-Mulhem ICS535-101ICS535-101 11

An Introduction to PrologAn Introduction to Prolog

Page 2: An Introduction to Prolog

Dr. Muhammed Al-MulhemDr. Muhammed Al-Mulhem ICS535-101ICS535-101 22

Prolog statementsProlog statements

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

Page 3: An Introduction to Prolog

Dr. Muhammed Al-MulhemDr. Muhammed Al-Mulhem ICS535-101ICS535-101 33

FactsFactsRepresent statements that are always true.Represent statements that are always true.The parameters are usually (but not always) constantsThe parameters are usually (but not always) constantsExamples:Examples:– female(mary).female(mary).– male(bill)male(bill)– male(jake)male(jake)– father( bill, jake).father( bill, jake).– mother( mary , jake).mother( mary , jake).

These simple structure state certain facts about jake, bill and mary.These simple structure state certain facts about jake, bill and mary.Note that these Prolog facts have no intrinsic semantics. They mean Note that these Prolog facts have no intrinsic semantics. They mean whatever the programmer wants them to mean.whatever the programmer wants them to mean.For example father( bill, jake). Could mean:For example father( bill, jake). Could mean:

– Bill and jake have the same fatherBill and jake have the same father– Jake is the father of billJake is the father of bill

The most common meaning is that bill is the fatehr of jake.The most common meaning is that bill is the fatehr of jake.

Page 4: An Introduction to Prolog

Dr. Muhammed Al-MulhemDr. Muhammed Al-Mulhem ICS535-101ICS535-101 44

Facts (contd.)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).

Page 5: An Introduction to Prolog

Dr. Muhammed Al-MulhemDr. Muhammed Al-Mulhem ICS535-101ICS535-101 55

RulesRulesThis is the other basic form of Prolog statement.This is the other basic form of Prolog statement.Used to construct the database corresponds of Used to construct the database corresponds of facts.facts.

It is a headed Horn clauseIt is a headed Horn clause

Use Use :-:- instead of instead of and a and a commacomma instead of a instead of a Right side: Right side: antecedentantecedent ( (ifif part) part)– May be single term or conjunctionMay be single term or conjunctionLeft side: Left side: consequentconsequent ((thenthen part) part)– Must be single termMust be single term

Page 6: An Introduction to Prolog

Dr. Muhammed Al-MulhemDr. Muhammed Al-Mulhem ICS535-101ICS535-101 66

RulesRulesparent(kim,kathy):- mother(kim,kathy).parent(kim,kathy):- mother(kim,kathy).

Can use variables (Can use variables (universal objectsuniversal objects) to generalize meaning:) to generalize meaning:

parent(X,Y):- mother(X,Y).parent(X,Y):- mother(X,Y).sibling(X,Y):- mother(M,X),sibling(X,Y):- mother(M,X), mother(M,Y),mother(M,Y), father(F,X),father(F,X), father(F,Y).father(F,Y).

Page 7: An Introduction to Prolog

Dr. Muhammed Al-MulhemDr. Muhammed Al-Mulhem ICS535-101ICS535-101 77

Rules (contd.)Rules (contd.)

Example Rules:Example Rules:path(X,Y) :- link(X,Z) , link(Z,Y).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).

Page 8: An Introduction to Prolog

Dr. Muhammed Al-MulhemDr. Muhammed Al-Mulhem ICS535-101ICS535-101 88

GoalsGoalsFacts and rules are used to describe both known facts and rules that Facts and rules are used to describe both known facts and rules that describe logical relationships among facts.describe logical relationships among facts.

These statements are the basis for the theorem proving model.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 The theorem is in the form of a proposition that we want the system to either prove or disprove.either prove or disprove.

In Prolog, these propositions are called goals.In Prolog, these propositions are called goals.

A series of one or more propositions, separated by commasA series of one or more propositions, separated by commas

Should be thought of as a queryShould be thought of as a query

If the parameters of the goal If the parameters of the goal are all constantsare all constants, then the answer to the , then the answer to the query should be “query should be “YesYes” or “” or “NoNo””

If the parameters of the goal contain variable(s), then the answer to the 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, query should be all the values for those variables which satisfy the query, or “No” if no value satisfies it.or “No” if no value satisfies it.

Page 9: An Introduction to Prolog

Dr. Muhammed Al-MulhemDr. Muhammed Al-Mulhem ICS535-101ICS535-101 99

Example:Example:– link(algo60,L) , link(L,M).link(algo60,L) , link(L,M).

/* “Are there some values for L and M such that algo60 is /* “Are there some values for L and M such that algo60 is linked to L and L is linked to M?” */linked to L and L is linked to M?” */

==========================================================================================

– male(ahmad).male(ahmad). /* Answer should be Yes/No */ /* Answer should be Yes/No */

– father(X,ali).father(X,ali). /* Answer should be X = “ ??” or No *//* Answer should be X = “ ??” or No */

– father(ali,naser).father(ali,naser). /* Answer should be Yes/No *//* Answer should be Yes/No */

– father(bill,X), mother(mary,X).father(bill,X), mother(mary,X).

/* Answer should be X = “??? or NO*//* Answer should be X = “??? or NO*/

GoalsGoals

Page 10: An Introduction to Prolog

Dr. Muhammed Al-MulhemDr. Muhammed Al-Mulhem ICS535-101ICS535-101 1010

Prolog ProgramsProlog ProgramsAre a series of facts and/or rules.Are a series of facts and/or rules.

Can be placed in any order, due to the Can be placed in any order, due to the nonprocedural nature of logic-based nonprocedural nature of logic-based languageslanguages

Are “executed” in a Prolog environment Are “executed” in a Prolog environment using goal statements.using goal statements.

Page 11: An Introduction to Prolog

Dr. Muhammed Al-MulhemDr. Muhammed Al-Mulhem ICS535-101ICS535-101 1111

Inferencing Process of PrologInferencing Process of PrologIf a goal is a compound proposition, each of the facts is a subgoal.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 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 inference rules and/or facts in the database that connect the goal to one or more facts in the database. one or more facts in the database. For example, if Q is a goal, then either Q must be found as a fact in 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 the database or the inferencing process must find a fact P1 and a sequence of propositions P2, P3, …Pn such thatsequence of propositions P2, P3, …Pn such thatP2 :- P1.P2 :- P1.P3 :- P2.P3 :- P2.……Q :- Pn.Q :- Pn.

Process of proving a subgoal is called Process of proving a subgoal is called matchingmatching, , satisfyingsatisfying, or , or resolutionresolution

Page 12: An Introduction to Prolog

Dr. Muhammed Al-MulhemDr. Muhammed Al-Mulhem ICS535-101ICS535-101 1212

ExampleExampleConsider this goalConsider this goal

man(bob)man(bob)This goal is compared with the facts and rules in the This goal is compared with the facts and rules in the database. If the database includes the factdatabase. If the database includes the fact

man(bob)man(bob)The proof is trivial—Yes.The proof is trivial—Yes.If , however, the database contains the following fact If , however, the database contains the following fact and rule.and rule.

father(bob)father(bob)man(X) :- father(X)man(X) :- father(X)

Prolog should find these two statements and use them Prolog should find these two statements and use them to infere truth of the goal.to infere truth of the goal.

Page 13: An Introduction to Prolog

Dr. Muhammed Al-MulhemDr. Muhammed Al-Mulhem ICS535-101ICS535-101 1313

Trace ExampleTrace Example

Page 14: An Introduction to Prolog

Dr. Muhammed Al-MulhemDr. Muhammed Al-Mulhem ICS535-101ICS535-101 1414

Inferencing Process of PrologInferencing Process of PrologBottom-up resolution, forward chainingBottom-up resolution, forward chaining– Begin with facts and rules of database and attempt Begin with facts and rules of database and attempt

to find sequence that leads to goalto find sequence that leads to goal– works well with a large set of possibly correct works well with a large set of possibly correct

answersanswers

Top-down resolution, backward chainingTop-down resolution, backward chaining– begin with goal and attempt to find sequence that begin with goal and attempt to find sequence that

leads to set of facts in databaseleads to set of facts in database– works well with a small set of possibly correct works well with a small set of possibly correct

answersanswers

Prolog implementations use backward chainingProlog implementations use backward chaining

Page 15: An Introduction to Prolog

Dr. Muhammed Al-MulhemDr. Muhammed Al-Mulhem ICS535-101ICS535-101 1515

When goal has more than one subgoal, can use eitherWhen goal has more than one subgoal, can use either

– Depth-first search: find a complete proof for the first Depth-first search: find a complete proof for the first subgoal before working on otherssubgoal before working on others

– Breadth-first search: work on all subgoals in parallelBreadth-first search: work on all subgoals in parallel

Prolog uses depth-first searchProlog uses depth-first search

– Can be done with fewer computer resourcesCan be done with fewer computer resources

Inferencing Process of PrologInferencing Process of Prolog

Page 16: An Introduction to Prolog

Dr. Muhammed Al-MulhemDr. Muhammed Al-Mulhem ICS535-101ICS535-101 1616

With a goal with multiple subgoals, if fail to show With a goal with multiple subgoals, if fail to show truth of one of subgoals, reconsider previous truth of one of subgoals, reconsider previous subgoal to find an alternative solution: subgoal to find an alternative solution: backtrackingbacktracking..

Begin search where previous search left off.Begin search where previous search left off.

Can take lots of time and space because may Can take lots of time and space because may find all possible proofs to every subgoal.find all possible proofs to every subgoal.

Inferencing Process of PrologInferencing Process of Prolog

Page 17: An Introduction to Prolog

Dr. Muhammed Al-MulhemDr. Muhammed Al-Mulhem ICS535-101ICS535-101 1717

Simple ArithmeticSimple ArithmeticProlog supports integer variables and integer arithmeticProlog supports integer variables and integer arithmeticisis operator: takes an operator: takes an arithmetic expressionarithmetic expression as right as right operand and variable as left operandoperand and variable as left operand

A is B / 10 + C.A is B / 10 + C.Not the same as an assignment statement!Not the same as an assignment statement!Should Should notnot be done with parameters be done with parametersEither both sides must have all variables instantiated (in Either both sides must have all variables instantiated (in which case which case isis acts as a relational =) or just the lefthand acts as a relational =) or just the lefthand side is not instantiated (which means the lhs receives a side is not instantiated (which means the lhs receives a value)value)

Therefore, the following is Therefore, the following is nevernever appropriate: appropriate:– Sum is Sum + Number.Sum is Sum + Number.

Page 18: An Introduction to Prolog

Dr. Muhammed Al-MulhemDr. Muhammed Al-Mulhem ICS535-101ICS535-101 1818

Arithmetic ExampleArithmetic Example

Page 19: An Introduction to Prolog

Dr. Muhammed Al-MulhemDr. Muhammed Al-Mulhem ICS535-101ICS535-101 1919

Arithmetic ExampleArithmetic Example

Page 20: An Introduction to Prolog

Dr. Muhammed Al-MulhemDr. Muhammed Al-Mulhem ICS535-101ICS535-101 2020

RecursionRecursionIs the only way to do iteration in PrologIs the only way to do iteration in Prolog

Is usually accomplished with at least one fact and one ruleIs usually accomplished with at least one fact and one rule

Example: Consider the following mathematical definition Example: Consider the following mathematical definition of factorial:of factorial:– 0! = 10! = 1– n! = (n-1)! * n n! = (n-1)! * n n > 0 n > 0

Here is the equivalent Prolog statements:Here is the equivalent Prolog statements:– fact(0,1).fact(0,1).– fact(N,NFact) :- N > 0, N1 is N-1,fact(N,NFact) :- N > 0, N1 is N-1, fact(N1,N1Fact), NFact is N1Fact * N. fact(N1,N1Fact), NFact is N1Fact * N.

Page 21: An Introduction to Prolog

Dr. Muhammed Al-MulhemDr. Muhammed Al-Mulhem ICS535-101ICS535-101 2121

List StructuresList StructuresThe value of a list consists of zero or more elements, The value of a list consists of zero or more elements, separated by commas and enclosed in square brackets.separated by commas and enclosed in square brackets.

Example: Example: [apple, prune, grape, kumquat][apple, prune, grape, kumquat]

Each element can be an atom or a listEach element can be an atom or a list

A variable such as A variable such as LL can be used to represent an entire list can be used to represent an entire list in a statement.in a statement.

The expression The expression [E][E] in a statement denotes a one-element in a statement denotes a one-element list.list.

The expression The expression [ ][ ] in a statement denotes an empty list. in a statement denotes an empty list.

Page 22: An Introduction to Prolog

Dr. Muhammed Al-MulhemDr. Muhammed Al-Mulhem ICS535-101ICS535-101 2222

The expression [X | Y] in a statement denotes a list with The expression [X | Y] in a statement denotes a list with one or more elements where the first element is the one or more elements where the first element is the head X and the rest of the list (which may be empty) is head X and the rest of the list (which may be empty) is the tail Y.the tail Y.

– This is how recursion can be used to traverse each This is how recursion can be used to traverse each element of a list.element of a list.

– X is called the “car” and Y is called the “cdr”.X is called the “car” and Y is called the “cdr”.(These terms are from Lisp.)(These terms are from Lisp.)

– For example, in [apple, prune, grape, kumquat], apple For example, in [apple, prune, grape, kumquat], apple is the car, and [prune, grape, kumquat] is the cdr.is the car, and [prune, grape, kumquat] is the cdr.

List StructuresList Structures

Page 23: An Introduction to Prolog

Dr. Muhammed Al-MulhemDr. Muhammed Al-Mulhem ICS535-101ICS535-101 2323

List StructuresList StructuresA list can be created with simple proposition.A list can be created with simple proposition.

new_list ([apple, prune, grape])new_list ([apple, prune, grape])This does the kind of thing that the propositionThis does the kind of thing that the proposition

male(ahmad) does.male(ahmad) does.We could have a second proposition likeWe could have a second proposition like

new_list ([ apricot, peach, pear])new_list ([ apricot, peach, pear])In goal mode, the list can be dismantled into head and tail.In goal mode, the list can be dismantled into head and tail.

new_list ([ Head, Tail])new_list ([ Head, Tail])Then Head is instantiated to apricot, and Tail to [peach, pear]Then Head is instantiated to apricot, and Tail to [peach, pear]The | can specify a list construction or a list dismanteling. Note that the The | can specify a list construction or a list dismanteling. Note that the following are equivalent:following are equivalent:

new_list ([ apricot, peach, pear | [ ]])new_list ([ apricot, peach, pear | [ ]])new_list ([ apricot, peach | [pear]])new_list ([ apricot, peach | [pear]])new_list ([ apricot | [peach, pear]])new_list ([ apricot | [peach, pear]])

Page 24: An Introduction to Prolog

Dr. Muhammed Al-MulhemDr. Muhammed Al-Mulhem ICS535-101ICS535-101 2424

Example 1Example 1Appending two lists togetherAppending two lists together– append([ ],List,List).append([ ],List,List).

– append([Head|List_1],List_2,[Head|List_3]) :- append(List_1,List_2, List_3).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 first one specifies that when the empty list is appended to any other list, that list is the result.the result.

The second one specifies several characteristics of the new list.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 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 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 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).(List_2) appended to it to form the tail (List_3).

Page 25: An Introduction to Prolog

Dr. Muhammed Al-MulhemDr. Muhammed Al-Mulhem ICS535-101ICS535-101 2525

Example 1Example 1

Page 26: An Introduction to Prolog

Dr. Muhammed Al-MulhemDr. Muhammed Al-Mulhem ICS535-101ICS535-101 2626

Reversing a listReversing a list– reverse([ ], [ ]).reverse([ ], [ ]).

– reverse([Head|Tail], List) :- reverse([Head|Tail], List) :- reverse(Tail,Result), append(Result, reverse(Tail,Result), append(Result, [Head],List).[Head],List).

Example 2Example 2

Page 27: An Introduction to Prolog

Dr. Muhammed Al-MulhemDr. Muhammed Al-Mulhem ICS535-101ICS535-101 2727

Example 2Example 2

Page 28: An Introduction to Prolog

Dr. Muhammed Al-MulhemDr. Muhammed Al-Mulhem ICS535-101ICS535-101 2828

Seeing if a list has a particular memberSeeing if a list has a particular member

– member(Element,[Element| _ ]).member(Element,[Element| _ ]).

– member(Element,[ _|List] :-member(Element,[ _|List] :-member(Element,List).member(Element,List).

The The _ _ is an “anonymous” variable; i.e., we don’t is an “anonymous” variable; i.e., we don’t care what the value is, although a value does care what the value is, although a value does need to be there.need to be there.

Example 3Example 3

Page 29: An Introduction to Prolog

Dr. Muhammed Al-MulhemDr. Muhammed Al-Mulhem ICS535-101ICS535-101 2929

Example 3Example 3

Page 30: An Introduction to Prolog

Dr. Muhammed Al-MulhemDr. Muhammed Al-Mulhem ICS535-101ICS535-101 3030

Example 4Example 4

Definition of sum function:Definition of sum function:

sum([],0).sum([],0).

sum([H|T],N):-sum(T,M), N is H+M.sum([H|T],N):-sum(T,M), N is H+M.

Page 31: An Introduction to Prolog

Dr. Muhammed Al-MulhemDr. Muhammed Al-Mulhem ICS535-101ICS535-101 3131

Example 6Example 6Definition of findOccurrences Definition of findOccurrences

function:function:

findOccurrences(X,[],0).findOccurrences(X,[],0).

findOccurrences(X,[X|T],N):- findOccurrences(X,[X|T],N):- findOccurrences(X,T,Z), N is Z+1.findOccurrences(X,T,Z), N is Z+1.

findOccurrences(X,[_|T],N):-findOccurrences(X,[_|T],N):-findOccurrences(X,T,Z), N is Z.findOccurrences(X,T,Z), N is Z.

Page 32: An Introduction to Prolog

Dr. Muhammed Al-MulhemDr. Muhammed Al-Mulhem ICS535-101ICS535-101 3232

Useful ExercisesUseful ExercisesWrite a Prolog functor that interleaves two lists. For example given Write a Prolog functor that interleaves two lists. For example given the query:the query:

?- interleave([1,2,3,4,5],[6,7,8,9,10],X).?- interleave([1,2,3,4,5],[6,7,8,9,10],X).It should return It should return X = [1,6,2,7,3,8,4,9,5,10]X = [1,6,2,7,3,8,4,9,5,10]

Write a Prolog functor that succeeds if its list input consists of Write a Prolog functor that succeeds if its list input consists of palindrome values. For example given the query:palindrome values. For example given the query:?- palindrome([1,2,3,4,5,4,3,2,1]).?- palindrome([1,2,3,4,5,4,3,2,1]).It should return Yes.It should return Yes.

Write functors to compute:Write functors to compute:– the the FibonacciFibonacci function function– xxyy for integers x and y. for integers x and y.

Page 33: An Introduction to Prolog

Dr. Muhammed Al-MulhemDr. Muhammed Al-Mulhem ICS535-101ICS535-101 3333

Example 5Example 5Definition of diffList function:Definition of diffList function:

diffList([], List, []).diffList([], List, []).

diffList([H|L1], L2, L3) :- not(member(H,L2)), diffList([H|L1], L2, L3) :- not(member(H,L2)),

diffList (L1, L2, L4), append([H],L4,L3).diffList (L1, L2, L4), append([H],L4,L3).

diffList([_|L1], L2, L3) :- diffList (L1, L2, L3).diffList([_|L1], L2, L3) :- diffList (L1, L2, L3).

Page 34: An Introduction to Prolog

Dr. Muhammed Al-MulhemDr. Muhammed Al-Mulhem ICS535-101ICS535-101 3434

Deficiencies of PrologDeficiencies of Prolog

Resolution order controlResolution order control

The closed-world assumptionThe closed-world assumption

The negation problemThe negation problem

Intrinsic limitationsIntrinsic limitations

Page 35: An Introduction to Prolog

Dr. Muhammed Al-MulhemDr. Muhammed Al-Mulhem ICS535-101ICS535-101 3535

Resolution Order Control Resolution Order Control

Depth-first search method can cause infinite recursionDepth-first search method can cause infinite recursion

– Example:Example:

ancestor(X,X).ancestor(X,X).

ancestor(X,Y) :- ancestor(Z,Y), parent(X,Z).ancestor(X,Y) :- ancestor(Z,Y), parent(X,Z).

– Keeps trying to satisfy the second ruleKeeps trying to satisfy the second rule

– Can be solved by reversing the two propositions on Can be solved by reversing the two propositions on the right, but that is against the basic nonprocedural the right, but that is against the basic nonprocedural philosophy of Prologphilosophy of Prolog

Deficiencies of PrologDeficiencies of Prolog

Page 36: An Introduction to Prolog

Dr. Muhammed Al-MulhemDr. Muhammed Al-Mulhem ICS535-101ICS535-101 3636

Resolution Order Control (Cont.)Resolution Order Control (Cont.)

The cut operator ! The cut operator ! – Can eliminate backtrackingCan eliminate backtracking– Is useful when a proposition can only be satisfied onceIs useful when a proposition can only be satisfied once– Form is Form is a,b,!,c,da,b,!,c,d

If c is not satisfied, the statement cannot go back and find If c is not satisfied, the statement cannot go back and find another possible value for banother possible value for b

– Example:Example:member(Element, [Element | _ ]) :- !member(Element, [Element | _ ]) :- !member(Element, [ _ | List]) :- member(Element,List).member(Element, [ _ | List]) :- member(Element,List).

The change in the first statement assumes that the list The change in the first statement assumes that the list consists of unique members.consists of unique members.

– The cut operator also is contrary to the Prolog philosophy of The cut operator also is contrary to the Prolog philosophy of nonprocedural programmingnonprocedural programming

Deficiencies of PrologDeficiencies of Prolog

Page 37: An Introduction to Prolog

Dr. Muhammed Al-MulhemDr. Muhammed Al-Mulhem ICS535-101ICS535-101 3737

Close World Assumption Close World Assumption

• If Prolog has insufficient data to answer a If Prolog has insufficient data to answer a question, the answer is “no”, just as it would question, the answer is “no”, just as it would be if it had sufficient data to answer “no”.be if it had sufficient data to answer “no”.

Deficiencies of PrologDeficiencies of Prolog

Page 38: An Introduction to Prolog

Dr. Muhammed Al-MulhemDr. Muhammed Al-Mulhem ICS535-101ICS535-101 3838

The Negation ProblemThe Negation Problem

Consider the following statement:Consider the following statement:sibling(X,Y) :- parent(M,X), parent(M,Y).sibling(X,Y) :- parent(M,X), parent(M,Y).– Nothing keeps a person from being their own sibling!Nothing keeps a person from being their own sibling!

Can be solved with a not proposition:Can be solved with a not proposition:

sibling(X,Y) :- parent(M,X), parent(M,Y),not(X = Y).sibling(X,Y) :- parent(M,X), parent(M,Y),not(X = Y).

However, the not proposition is not a not operator (double However, the not proposition is not a not operator (double negation is not allowed), which causes some limitationsnegation is not allowed), which causes some limitations

Deficiencies of PrologDeficiencies of Prolog

Page 39: An Introduction to Prolog

Dr. Muhammed Al-MulhemDr. Muhammed Al-Mulhem ICS535-101ICS535-101 3939

Intrinsic LimitationsIntrinsic Limitations• Prolog is often not efficientProlog is often not efficient• Example:Example:

sorted([ ]).sorted([ ]).sorted([x].sorted([x].

sorted([x, y | list]) :- x <= y, sorted([y | list]).sorted([x, y | list]) :- x <= y, sorted([y | list]).– All permutations of list must be tried until the All permutations of list must be tried until the

right one is found. right one is found.

Deficiencies of PrologDeficiencies of Prolog

Page 40: An Introduction to Prolog

Dr. Muhammed Al-MulhemDr. Muhammed Al-Mulhem ICS535-101ICS535-101 4040

Applications of Logic Applications of Logic ProgrammingProgramming

Relational database management Relational database management systemssystems

Expert systemsExpert systems

Natural language processingNatural language processing

EducationEducation

Page 41: An Introduction to Prolog

Dr. Muhammed Al-MulhemDr. Muhammed Al-Mulhem ICS535-101ICS535-101 4141

ConclusionsConclusions

Advantages:Advantages:– Prolog programs based on logic, so likely to Prolog programs based on logic, so likely to

be more logically organized and writtenbe more logically organized and written

– Processing is naturally parallel, so Prolog Processing is naturally parallel, so Prolog interpreters can take advantage of multi-interpreters can take advantage of multi-processor machinesprocessor machines

– Programs are concise, so development time is Programs are concise, so development time is decreased – good for prototypingdecreased – good for prototyping

Page 42: An Introduction to Prolog

Dr. Muhammed Al-MulhemDr. Muhammed Al-Mulhem ICS535-101ICS535-101 4242

SummarySummaryPredicate calculus provides a formal means for logical Predicate calculus provides a formal means for logical expressions (I.e. those that evaluate to true or false)expressions (I.e. those that evaluate to true or false)

Horn Clauses provide a particular structure which can be Horn Clauses provide a particular structure which can be used for most logical expressionsused for most logical expressions

Declarative semantics allows both for the focus of Declarative semantics allows both for the focus of problem solving to be on “what” rather than “how” and for problem solving to be on “what” rather than “how” and for nonprocedural programmingnonprocedural programming

Prolog uses declarative semanticsProlog uses declarative semantics

There are some deficiencies in Prolog, some of which There are some deficiencies in Prolog, some of which are inherent to declarative semanticsare inherent to declarative semantics