# weakness of logic programming

Post on 02-Jan-2016

21 views

Category:

## Documents

Embed Size (px)

DESCRIPTION

Weakness of logic Programming. Horn clause cannot cover all logics every living thing is an animal or a plant animal(X) V plant(X) living(X) So we must have the following definition in Prolog animal(X):- living(X), not plant(X). plant(X):- living(X), not animal(X). - PowerPoint PPT Presentation

TRANSCRIPT

• Weakness of logic ProgrammingHorn clause cannot cover all logicsevery living thing is an animal or a plantanimal(X) V plant(X) living(X)

So we must have the following definition in Prologanimal(X):- living(X), not plant(X).plant(X):- living(X), not animal(X).Prolog can only have one element on this sideNot will only satisfy if we cant find that X is an animal.

• Another weakness is the way that Prolog uses its rules from top to bottom. We already saw the problemnot means cannot find. It does not mean false.

• exercisesBinary search Tree nilt(L,M,R)

• Controlling backtrackingWe can prevent backtracking, by using a feature called cut.Normally, prolog backtrack to find all answers, but sometimes we only need the first solution.Examplef(X,0):-X
• If we try?-f(1,Y), 2
• f(X,0):-X
• f(X,0):-X
• Lets try another example?-f(7,Y)Y = 4. (this is the answer)First, try rule 1.X
• We can optimize it further.Notice that if X
• Be careful now. If we do not have cut, we may get several solutions, some of which may be wrong. For example:?-f(1,Y)Y=0 ;Y=2 ;Y=4

f(X,0):-X

• When finding answer up to cut, the system has already found solutions of B1 to Bm. When the cut is executed, solutions B1 to Bm are considered fix. For example:C:-P,Q,R,!,S,T,U.C:-V.A:-B,C,D.?-A.When ! Is executed. P,Q,R will be fixed (no backtracking) and C:-V. will never get executed.Backtracking is still possible in S,T,U but will never go back beyond the !

H:-B1, B2, , Bm, !, , Bn.We put the question here.

• C:-P,Q,R,!,S,T,U.C:-V.A:-B,C,D.?-A.AB, C, DP,Q,R,!,S,T,U.Can backtrack if not reach !Can backtrack to ! But not beyond thatLets see another example next page.

• The rules are mutually exclusive.So we can use else if for efficiency.max(X,Y,X):-X=>Y, !.max(X,Y,Y).Be careful of the following situation:?-max(3,1,1).yes. It does not get matched with the first rule from the start.But it then matches the second rule, because we already took out the conditional test.So we fix it in the next page.

max(X,Y,X):-X=>Y.max(X,Y,Y):-X

• max(X,Y,Max):- X=>Y, !, Max =X.max(X,Y,Max):- Max =Y.max(X,Y,X):-X=>Y, !.max(X,Y,Y).?-max(3,1,1).Program is able to enter the first rule because Max is not matched to any value.Once X=>Y in the first rule is satisfied, there is no backtracking.Lets see an example on list next page.

• If there are many X in the list, this definition allow any X to be returned when we order it to backtrack.If we want to make sure that only the first occurrence of X will be returned, we will have to use cut.member(X, [X|L]).member(X, [Y|L]):-member(X, L).member(X, [X|L]):-!member(X, [Y|L]):-member(X, L).?-member(X, [a,b,c]).X=a;noOrder it to find another solution (backtrack)

This is just one solution

• Be careful.If we define things using cut, we normally intend that every rule is tried, right from the first rule. So cut can serve its purpose.Any query that allows rule skipping will destroy everything. ?add(a,[a],[a,a]).yesDoes not match the first rule, but match the second rule. This makes things incorrect.

• tennis match examplebeat(tom, jim).beat(ann, tom).beat(pat, jim).We want to categorize players into 3 types:Winner: win every gameFighter: player with mixed recordsSportman: lose every game

• X is a fighter if X is a winner ifWe can use if then else:class(X,fighter):- beat(X,_), beat(_,X), !.class(X,winner):-beat(X,_), !.class(X,sportman):-beat(_,X).beat(tom, jim).beat(ann, tom).beat(pat, jim).We still need to be careful about rules getting skipped. For example:?-class(tom,sportman).yes (but in fact tom is a fighter)

• ?-p(X).X=1;X=2;nop(1).p(2):-!.p(3).?-p(X),p(Y).X=1, Y=1;X=1, Y=2;X=2, Y=1;X=2, Y=2;no?-p(X),!, p(Y).X=1, Y=1;X=1, Y=2;no

• class(0,zero):-!.class(Number,positive):- Number>0, !.class(Number,negative).

class(Number,positive):- Number>0.class(0,zero).class(Number,negative):-Number

• Splitting a list to list of positive (including 0) and negative numbers.split([],[],[]).split([H|T],[H|Z],R):- H=>0,split(T,Z,R).split([H|T],R,[H|Z]):-H0,!, split(T,Z,R).split([H|T],R,[H|Z]):- split(T,R,Z).Careful:?-split([8,1,-3,0,-4,5],[1,0,5],[8,-3,-4]).Yes (it matches rule 3, skipping earlier rules.)More difficult to understand

• Make it bettersplit([],[],[]).split([H|T],[H|Z],R):- H=>0,!, split(T,Z,R).split([H|T],R,[H|Z]):- H0,!, split(T,Z,R).split([H|T],R,[H|Z]):- H
• Unnecessary cutsplit([],[],[]):- !split([H|T],[H|Z],R):- H=>0,!, split(T,Z,R).split([H|T],R,[H|Z]):- H
• Multiple choice with cutsquint([],[]).sqluint([X|T],[Y|L]):- integer(X), !, Y is X*X, squint(T,L).sqluint([X|T],[X|L]):-squint(T,L).

The third clause is not used when the goal backtracks. But it still does not stand independently.

• Partial mapsevens([],[]).evens([X|T],[X|L]):- 0 is X mod 2, evens(T,L).evens([X|T],L):- 1 is X mod 2, evens(T,L).

evens([],[]).evens([X|T],[X|L]):- 0 is X mod 2, !, evens(T,L).evens([X|T],L):- 1 is X mod 2, evens(T,L).

• setify([],[]).setify([X|T],L):- member(X,T), setify(T,L).setify([X|T],[X|L]):- setify(T,L).setify([],[]).setify([X|T],L):- member(X,T), !, setify(T,L).setify([X|T],[X|L]):- setify(T,L).

• treeinsert(I,[], n(I,[],[])).insert(I, n(N,L,R) , n(N,L1,R)):- IN, !, insert(I, R, R1).insert(I, n(I,L,R) , n(I,L,R)).

• Negation as failurelikes(mary,X):-snake(X),!,fail.likes(mary,X):-animal(X).Or

likes(mary,X):-snake(X),!,fail;animal(X).

Mary likes all animals but snake.

• different(X,X):- !, fail.different(X,Y).Ordifferent(X,Y):- X=Y, !, fail.;true.

• The not predicatenot(P):-P,!,fail;true.

Some prolog allows not p(X) or \+(p(X))

• member(X,[X|_]).member(X,[_|T]):-member(X,T).notmem(X,L):- \+(member(X,L)).

notmem1(X,[]).notmem1(X,[Y|T]):-X\==Y, notmem1(X,T).

• Same examples with the use of notlikes(mary,X):-animal(X), not snake(X).different(X,Y):- not X=Y.class(X,fighter):- beat(X,_), beat(_,X).class(X,winner):-beat(X,_), not beat(_,X).class(X,sportman):-beat(_,X), not beat(X,_).likes(mary,X):-animal(X), (snake(X), !, fail ; true).It means ->otherwise

• Conclusion about cutProsAvoid unnecessary execution.Use like if-then-elseConsThe rule must be tried in order, no skipping.P:-a,bP:-c.

P:- a,!,b.P:-c.

P:-c.P:-a,!,b.Logic is P (a ^ b) V cChanging order has no effect.Logic is P (a ^ b) V ((not a) ^ c)Logic is P c V (a ^ b)Different meaning

• goodteam(manunited).expensive(manunited).goodteam(liverpool).reasonable(T):- not expensive(T).

?-goodteam(X), reasonable(X).X=liverpool.

?- reasonable(X) , goodteam(X). no

Has assigned valueHas no assigned value. The not clause does not assign a value for you. See next page.

• ?- expensive(X).Means

?-not expensive(X).Means not So it gives no in the previous page.It is forall, so prolog cannot instantiate a single value.Only need to find a single value of X.

• Flight timetable(Place1, Place2, ListOfFlights)Flight = DepTime / ArrTime / FlightNo / ListOfDays

timetable(london, edinburgh, [9:40 / 10:50 / ba4733 / alldays, 19:40 / 20:50 / ba4833 / [mo,tu,we,th,fr,su]]).List of days or alldays

• route(P1, P2, Day, Route)

flight(P1, P2, Day, Num, DepTime, ArrTime)deptime(Route, Time)transfer(T1, T2)List of From / To / FlightNo / DepTime

• route(P1, P2, Day, Route):- flight(P1,P2,Day,Fnum,DepTime,_).

route(P1, P2, Day, [(P1/P3/F1/Dep1)|RestRoute]):-route(P3,P2,Day,RestRoute), flight(P1,P3,Day,F1,Dep1,Arr1),deptime(RestRoute,Dep2),transfer(Arr1, Dep2).

flight(P1, P2, Day, Fnum, DepTime, ArrTime):-timetable(P1,P2, FlightList).member(DepTime / ArrTime / Fnum / Daylist, FlightList),flyday(Day,Daylist).

deptime([P1 / P2 / Fnum / Dep | _ ], Dep).

transfer(H1:M1, H2:M2):-60*(H2-H1) + M2 M1 >= 40.

• Logic circuitABBACinv(0,1).inv(1,0).and(1,1,1).and(1,0,0).and(0,1,0).and(0,0,0).