a) Define an operator that counts the number of all occurrences of a given item (atom or tuple, ground or non-ground) as a top-level element of a given tuple. (Hint: Try to adapt the recursive structure of repall in opstup.bat.) The operator may either be a function occfun(item,tuple) or a relation occrel(item,tuple,occurrences). For instance, the occfun-function call occfun(oranges,[apples,oranges,bananas,oranges,oranges,peaches]) should return 3, while occrel(oranges,[apples,oranges,bananas,oranges,oranges,peaches],Occ) should bind Occ to 3. In either version, what is the result of a call with the item [_,_] and the tuple [[apples],[apples,oranges],[X,Y],[oranges,peaches],]? What happens if the tuple is a variable?
b) Several times we needed a tuple-appending (or -concatenating) function appfun such that, in "..."-notation, appfun([a1,...,aM],[b1,...,bN]) returns the tuple [a1,...,aM,b1,...,bN] of length M+N, (informally, the adjacent brackets "],[" are reduced to the comma). Note that (1), even for N=0, [a1,...,aM|[b1,...,bN]] = [a1,...,aM,b1,...,bN] (informally, the adjacent "|[" and the corresponding "]" are erased), and (2) len([a1,...,aM]) = M is our builtin for efficient tuple-length computation. Now consider the following "..."-less equations about appfun:
Define a tuple-appending relation apprel such that apprel([a1,...,aM],[b1,...,bN],App) binds App to [a1,...,aM,b1,...,bN]. What is the (deterministic or non-deterministic?) result of an "inverse" call of the form apprel(TupA,TupB,[a1,...,aM,b1,...,bN])? What is the value of len(tupof(apprel(TupA,TupB,[apples,bananas,oranges,peaches])))?
c) In "A comparison of SQL and RelFun" (sql.script) we used the following member definition:
az member(X,[X|_])!. az member(X,[_|Y]) :- member(X,Y).The "!" makes the member relation deterministic by `cutting' computation after its first clause (a fact), so that the second clause (a rule) cannot be used in the successful case that the "search item" X also appears as the first element of the "searched tuple" [X|_]. Reformulate this as an equivalent definition using naf (our negation-as-failure primitive) instead of "!" (the cut symbol). (Hint: First replace one anonymous variable "_" by a named one such as U). Change the member definition into a non-deterministic version; compare and explain the difference for a ground call (containing no variable) and for the non-ground call (containing the variable What) member(What,[i,s,b,a]).
Which member version should be used for the RelFun query below such that it becomes equivalent to the SQL query below? Would the non-deterministic member version change any query result of the dialog sql.bat?
% SQL: % SELECT Position % FROM Employee_statistics % WHERE Salary IN (75000,65000,45000); % RelFun: tupof(employee_statistics(_,Salary,_,Position), member(Salary,[75000,65000,45000]), Position)Formulate the above equivalent queries as an equivalent English sentence. Can, for the database given by sql.bat, the above SQL query be rewritten using BETWEEN instead of IN?
d) The fragment of the NUTRIMINE information system on pages 23-28 of Knowledge Bases in the World Wide Web consists of a purely relational and a mixed functional-relational version. Which clauses would have to be changed to produce a purely functional version? `Functionalize' these clauses. (Hint: Rewrite facts as constantly true functions and 1-premise ":-"-rules as ":&"-equations.)
Execute nutrimine.bat. Enrich the call nutramount(strawberry) by a suitably parameterized qsort call such that the nutrient-amount pairs are ordered according to decreasing mg/kg amounts.