%% User-Defined Aggregates % Schema %------- database({sppp(S:string, Item:string, Price:integer)}). %Derivated predicates and rules %------------------------------ % General format of user-defined aggregates is % single(newaggr, Y, NV) and multi(newaggr, Y, OV, NV) % % Built-In aggregates `max' and `min' defined as % single(max, Y, Y). % multi(max, Y, MO, MN) <- Y > MO, MN=Y. % multi(max, Y, MO, MN) <- Y <= MO, MN=MO. % % single(min, Y, Y). % multi(min, Y, MO, MN) <- Y >= MO, MN=MO. % multi(min, Y, MO, MN) <- Y < MO, MN=Y. % % `count' is defined as % single(count, Y, 1). % multi(count, Y, Old, New) <- New= Old+1. % % `sum' is defined as % single(sum, Y, Y). % multi(sum, Y, Old, New) <- New= Old+Y. % % now an example findmax(S, mymax<(Itm,Pric)>) <- sppp(S, Itm, Pric). single(mymax, (Item, Pr), (Item, Pr)). multi(mymax, (Sit,Sp),(Oit,Op), (Sit, Sp)) <- Sp >= Op. multi(mymax, (Sit,Sp),(Oit,Op), (Oit, Op)) <- Sp < Op. export findmax(S, M).