CS 541 Lecture -*- Outline -*- * non-logical features of logic programming languages ** Cut (!) the only control operator in Prolog reduces the search space by dynamically pruning the search tree green cut: prunes fruitless paths that cannot produce solutions red cut: prunes paths that may contain solutions (negation) ------------------------------------------ sig cut_examples. type mbr T -> (list T) -> o. type mbr1 T -> (list T) -> o. type mbr2 T -> (list T) -> o. module cut_examples. mbr Head (Head::Tail). mbr Element (Head::Tail) :- mbr Element Tail. % membership test only mbr1 Head (Head::Tail) :- !. mbr1 Element (Head::Tail) :- mbr Element Tail. % discussed in class... mbr2 Head (Head::Tail). mbr2 Element (Head::Tail) :- !, mbr Element Tail. ------------------------------------------ If only want membership test, want to stop as soon as found something. use cut (!, in first clause) second clause used only if first fails. cannot generate several different answers. do the following... [cut_examples] ?- mbr 2 X. The answer substitution: X = 2 :: _T1 More solutions (y/n)? y The answer substitution: X = _T1 :: 2 :: _T2 More solutions (y/n)? y The answer substitution: X = _T1 :: _T2 :: 2 :: _T3 More solutions (y/n)? y The answer substitution: X = _T1 :: _T2 :: _T3 :: 2 :: _T4 More solutions (y/n)? n yes [cut_examples] ?- mbr1 2 X. The answer substitution: X = 2 :: _T1 More solutions (y/n)? y no (more) solutions [cut_examples] ?- mbr2 2 X. The answer substitution: X = 2 :: _T1 More solutions (y/n)? y The answer substitution: X = _T1 :: 2 :: _T2 More solutions (y/n)? y The answer substitution: X = _T1 :: _T2 :: 2 :: _T3 More solutions (y/n)? y The answer substitution: X = _T1 :: _T2 :: _T3 :: 2 :: _T4 More solutions (y/n)? n yes [cut_examples] ?- mbr2 2 X. The answer substitution: X = 2 :: _T1 More solutions (y/n)? y The answer substitution: X = _T1 :: 2 :: _T2 More solutions (y/n)? y The answer substitution: X = _T1 :: _T2 :: 2 :: _T3 More solutions (y/n)? y The answer substitution: X = _T1 :: _T2 :: _T3 :: 2 :: _T4 More solutions (y/n)? n yes [cut_examples] ?- mbr X (1::2::3::nil). The answer substitution: X = 1 More solutions (y/n)? y The answer substitution: X = 2 More solutions (y/n)? y The answer substitution: X = 3 More solutions (y/n)? y no (more) solutions [cut_examples] ?- mbr1 X (1::2::3::nil). The answer substitution: X = 1 More solutions (y/n)? y no (more) solutions [cut_examples] ?- mbr2 X (1::2::3::nil). The answer substitution: X = 1 More solutions (y/n)? y The answer substitution: X = 2 More solutions (y/n)? y The answer substitution: X = 3 More solutions (y/n)? y no (more) solutions *** What does cut do? ! succeeds and commits Prolog to all the choices made since the parent goal was unified with the head of the clause in which the cut occurs **** cut prunes all clauses below it ------------------------------------------ CUT PRUNES ALL CLAUSES BELOW IT sig cut_examples2. type p o. type b o. type a o. type c o. type d o. type e o. type f o. type g o. type h o. module cut_examples2. p :- a, b, c. b :- d, e, !, f. b :- g, h. a. d. e. c. g. h. ------------------------------------------ cut prevents the second b rule from being used if d and e succeed Q: does the query b. succeed? How about p.? **** Backtracking across cut causes whole clause to fail (fail to caller) if f fails, then the whole clause fails. think of cut as committing Prolog to a choice the goal succeeds and commits Prolog to all the choices made since the parent goal was unified with the head of the clause in which the cut occured. thus: prunes all clauses below it prunes all alternatives to its left in a clause. does not affect goals to its right in a clause. example: If b1 then b2 else b3 can be done using cut as follows a1 :- b1, !, b2. a1 :- b3. Using the higher-order features, this can be done as ------------------------------------------ USING CUT TO DO IF THEN ELSE sig if. type if o -> o -> o -> o. module if. %%% ``if Test E1 E2'' does E1 when Test %%% succeeds, and otherwise does E2. %%% This uses cut! if Test E1 E2 :- Test, !, E1. if Test E1 E2 :- E2. ------------------------------------------ ** Negation as failure *** negation treated as unprovability (closed world assumption) not combines cut and fail (predicate that fails) ------------------------------------------ NEGATION IN (\)PROLOG not G :- G, !, fail. not G . ------------------------------------------ this is built-in to lambda Prolog. order essential (bad in itself, since less modular) idea is that if goal fails finitely, not succeeds but Prolog doesn't even find finite failures for sure note: if foo terminates, so does not(foo) if foo does not terminate, not(foo) may or may not halt *** good programming style avoids cut-fail, since logic disappears. difference between proving something false not being able to prove it's true(closed world) *** "not" doesn't work correctly for non-ground goals ------------------------------------------ NOT DOESN'T WORK CORRECTLY ON NON-GROUND GOALS sig negation_problem. kind person type. kind person type. type unmarried_student person -> o. type married person -> o. type student person -> o. type bill person. type joe person. module negation_problem. unmarried_student X :- (not (married X)), (student X). student bill. married joe. [negation_problem] ?- unmarried_student X. no (more) solutions [negation_problem] ?- unmarried_student bill. yes ------------------------------------------ *draw the search tree* for these! X=bill should be a solution. (not (married X)) tries (married X), finds X=joe, so married succeeds, causing (not (married X)) to fail (don't try again because of cut!) (solved by reversing order of goals) double negation doesn't work as one expects ** Other non-logical stuff in Prolog *** is: allows one to use computer's arithmetic ------------------------------------------ ACCESS TO MACHINE ARITHMETIC sig inc. type inc int -> int -> o. module inc. inc N M :- M is N + 1. [inc] ?- inc 2 M. The answer substitution: M = 3 More solutions (y/n)? y no (more) solutions [inc] ?- inc 2 3. yes [inc] ?- inc N 3. Error: is: Flexible term head in evaluation: _194 no (more) solutions ------------------------------------------ M is N + 1 is like assignment M := N + 1 here N needs to be already instantiated. *** asserta, assertz: put rules in database not in \Prolog but in \Prolog can use implication (=>) to do this temporarily! *** retract: takes rule out of database allow non-monotonic reasoning, memoization, assignment also not in \Prolog