Chapter 5 Scroll throgh definition and implementation of quadratic function g/2 Demo ?- g( 5, Y). ?- g( X, 25). ?- 3 < 4. ?- X < 4. X must be instantiated for the comparison to succeed. Slide, Figure 5.1 Scroll to implementation of f/2. Demo ?- f( 1, Y). ?- f( 5, Y). ?- f( 7, Y). ?- f( X, normal). ?- f( X, alert1). f( X, Y) assumes X is instantiated to a number. ?- f( 2, Y). ?- f( 2, Y), Y = alert1. Obviously fails. Slides, Analyze the backtracking All the rules are checked, even after the failure on the first rule. WE know that if X < 3 succeeds in the first rule, that it will fail all the following rules. But, PROLOG does not know that without trying all the following rules. A cut lets us tell Prolog to not even try any of the following rules. Scroll to implementation of f2/2. Demo ?- f2( 1, Y). ?- f2( 5, Y). ?- f2( 7, Y). ?- f2( 2, Y), Y = alert1. Results are the same. Slide, Figure 5.2, No cut in function f. The cut in function f2. Slides, trace backtracking with f2( 2, Y), Y = alert1. Third version, Why have the second test with 3? Scroll to implementation of f3/2. Demo ?- f3( 1, Y). ?- f3( 5, Y). ?- f3( 7, Y). Red cuts vs green cuts Slides, The meaning of the cut mechanism Demo ?- max( 3, 4, M). ?- max( 4, 3, M). Scroll to implementation of max/3 Scroll to max2/3 for a more efficient version Is the cut red or green? (red) Scroll to prediction of max( 3, 1, 1) and max2( 3, 1, 1). Demo ?- max( 3, 1, 1). ?- max2( 3, 1, 1). Scroll to max3/3 that corrects the problem Scroll to nondeterministic my_member/2 Scroll to deterministic my_member2/2 Demo ?- my_member( a, [a,b,c,a,b,c]). ?- my_member2( a, [a,b,c,a,b,c]). Demo review of built-in member ?- member( t, [c,a,t]). ?- member( t, [c,a,t,t,l,e]). member/2 is nondeterministic Scroll to specification of add Demo ?- add( a, [b,c,d], L). ?- add( a, [b,a,d], L). Scroll to the implementation of add Classification into categories Scroll to the specification of class( Player, Category) Category is the output variable Demo (predict) ?- class( tom, C). ?- class( ann, C). ?- class( pat, C). ?- class( jim, C). Scroll to implementation of rules for fighter winner and sportsman Shortcomings ?- class( jim, C). Gives sportsman twice ?- class( X, winner). Gives X = tom, who is not a winner ?- class( tom, winner). Gives yes, even though he is not | ?- class( Who, Category). Only gives Category = fighter Who = tom and that's all. With no backtracking there are no more solutions. Negation as failure Demo ?- true. ?- fail. true always succeeds. fail always fails. Scroll to specification and implementation of first version of likes/2 Demo ?- likes( mary, snake). ?- likes( mary, cat). Scroll to second version written as one clause. Demo ?- likes2( mary, snake). ?- likes2( mary, cat). No difference. But, predict and demo this ?- likes( mary, What). ?- likes2( mary, What). Failure is NOT logical Negation Demo ?- different( a, b). ?- different( b, a). ?- different( a, a). Scroll to specification and implementation of different/2, and second version Demo ?- not( true). ?- not( fail). ?- not( X = a). ?- not( b = a). Scroll to specification and implementation of not( Goal) Scroll to the gprolog \+ prefix operator, "cannot prove" Limitation on query ?- not( X). X must be instantiated to a goal. Demo, Note the errors in the following queries. ?- not( X). ?- \+ X. ?- X. ?- not( 5). ?- 5. X must be instantiated to a goal for the search to occur. Scroll to third version of likes/2 with no cuts Demo ?- likes3( mary, snake). ?- likes3( mary, cat). ?- likes3( mary, What). % fixed Scroll to third version of different/2 with no cuts Scroll to second version of class/2 with no cuts Demo | ?- class2( jim, C). % Still gives two solutions | ?- class2( tom, winner). % Correct now | ?- class2( Who, winner). % Correct now | ?- class2( Who, Category). % Gives all solutions correctly now Scroll to good_food, expensive facts and reasonable rule. Demo ?- good_food( X), cheap( X). ?- cheap( X), good_food( X). Why does the order matter? In the first query, cheap/1 searches with an instantiated value for X. In the second query, X is not instantiated yet. Demo ?- cheap( in_n_out). ?- cheap( geoffreys). ?- expensive( R). ?- cheap( R). The last query above does not find in_n_out because R is not instantiated to a goal. Slide, Problems with Negation