Listing example: use case for new feature in 8.3.5

listing/1 in versions 8.3.5 and forward now properly support partially ground heads, showing only the relevant clauses. Eric asked for an example on how this would be useful, and this post shows one useful scenario.

Suppose you have a large database with millions of products and thousands of categories of products. But products are only available if their corresponding warehouse is open.

Something like this:

product(ribeye,meat,33.33,barcode1)        :- warehouse(east,open).
product(pork,meat,33.33,barcode2)          :- warehouse(east,open).
product(lentils,grain,12.12,barcode3)      :- warehouse(west,open).
product(rice,grain,12.12,barcode4)         :- warehouse(west,open).
product(black_beans,grain,12.12,barcode5)  :- warehouse(west,open).
product(carrots,vegetable,13.13,barcode6)  :- warehouse(south,open).
product(lettuce,vegetable,13.13,barcode7)  :- warehouse(south,open).
product(peas,vegetable,13.13,barcode8)     :- warehouse(south,open).

Suppose you just want to look at the clauses for products of type grain, to see the dependencies on warehouses, then you can do:

1 ?- listing(product(_,grain,_,_)).
product(lentils, grain, 12.12, barcode3) :-
    warehouse(west, open).
product(rice, grain, 12.12, barcode4) :-
    warehouse(west, open).
product(black_beans, grain, 12.12, barcode5) :-
    warehouse(west, open).

and this will show you the logical dependencies for products of type grain; of course this can be easily programmed, but it is nice to have it of-the-shelf in listing/1. As you can see this would be useful if you have thousands of categories.

1 Like

I could not think of a nice title but you can edit it.