% A quadratic function, g( X, Y) % Y is the square of X g( X, Y) :- Y is X * X. % Figure 5.1, A pollution alert function. f( X, normal) :- X < 3. % Rule 1 f( X, alert1) :- 3 =< X, X < 6. % Rule 2 f( X, alert2) :- 6 =< X. % Rule 3 % Second version. Experiment 1. % More efficient with query % ?- f2( 2, Y), Y = alert1. % Green cuts. Could be removed. f2( X, normal) :- X < 3, !. % Rule 1 f2( X, alert1) :- 3 =< X, X < 6, !. % Rule 2 f2( X, alert2) :- 6 =< X. % Rule 3 % Third version. Experiment 2 % Logic: % if X < 3 then Y = normal, % otherwise if X < 6 then Y = alert1, % otherwise Y = alert2. % Red cuts. Could not be removed. f3( X, normal) :- X < 3, !. % Rule 1 f3( X, alert1) :- X < 6, !. % Rule 2 f3( _, alert2). % Rule 3 % max( X, Y, Max) % Max is the maximum of X and Y % First version. max( X, Y, X) :- X >= Y. max( X, Y, Y) :- X < Y. % Second version. Logic: % if X >= Y then Max = X, % otherwise Max = Y max2( X, Y, X) :- X >= Y, !. max2( _, Y, Y). % Predict max( 3, 1, 1) and max2( 3, 1, 1). % Third version. Corrects % the query ?- max2( 3, 1, 1). max3( X, Y, Max) :- X >= Y, !, Max = X ; Max = Y. % my_member( X, L) % Element X is a member of list L. % Nondeterministic, several solutions. my_member( X, [X|_]). my_member( X, [_|L]) :- my_member( X, L). % Second version. % Find only the first occurrence. my_member2( X, [X|_]) :- !. my_member2( X, [_|L]) :- my_member2( X, L). % add( X, L, L1) % if X is a member of L, then L1 = L, % otherwise L1 is equal to L with X inserted. add( X, L, L) :- member( X, L), !. add( X, L, [X|L]). % Classification into categories beat( tom, jim). beat( ann, tom). beat( pat, jim). % class( Player, Category) % There are three categories % winner: one who won all games % fighter: one who won some and lost some % sportsman: one who lost all games % Rule for fighter: % X is a fighter if % there is some Y such that X beat Y and % there is some Z such that Z beat X. % Rule for winner: % X is a winner if % X beat some Y % X was not beaten by anybody % If X beat somebody and X was beaten by somebody % then X is a fighter, % otherwise if X beat somebody % then X is a winner, % otherwise if X got beaten by somebody % then X is a sportsman. class( X, fighter) :- beat( X, _), beat( _, X), !. class( X, winner) :- beat( X, _), !. class( X, sportsman) :- beat( _, X). % Negation as failure animal( cat). animal( dog). animal( snake). % 'Mary likes all animals but snakes'. % Logic: % if X is a snake then 'Mary likes X' is not true, % otherwise if X is an animal then Mary likes X. % true always succeeds, and fail always fails. % Note typo in book for following rule. likes( mary, X) :- X = snake, !, fail. likes( mary, X) :- animal( X). % Second version. % Written as one clause. likes2( mary, X) :- X = snake, !, fail ; animal( X). % different( X, Y) % X and Y do not match. % If X and Y match then different( X, Y) fails, % otherwise different( X, Y) succeeds. different( X, X) :- !, fail. different( _, _). % Second version % Written as one clause different2( X, Y) :- X = Y, !, fail ; true. % not( Goal) % If Goal succeeds then not( Goal) fails, % otherwise not( Goal) succeeds. not( P) :- P, !, fail ; true. % gprolog does not have the not functor built-in. % It has the equivalent \+ prefix operator. % \+ is pronounced "cannot prove". % Third version of likes. likes3( mary, X) :- animal( X), \+ X = snake. % Third version of different. different3( X, Y) :- \+ X = Y. % Second version of class. % beat( tom, jim). % beat( ann, tom). % beat( pat, jim). % class( Player, Category) % There are three categories % winner: one who won all games % fighter: one who won some and lost some % sportsman: one who lost all games class2( X, fighter) :- beat( X, _), beat( _, X). class2( X, winner) :- beat( X, _), \+ beat( _, X). class2( X, sportsman) :- beat( _, X), \+ beat( X, _). % Problems with negation good_food( in_n_out). good_food( geoffreys). expensive( geoffreys). cheap( Restaurant) :- \+ expensive( Restaurant).