[m-users.] Inferring over dynamic data

Zoltan Somogyi zoltan.somogyi at runbox.com
Tue Jan 18 12:19:24 AEDT 2022


2022-01-18 09:44 GMT+11:00 "Stepp, Nigel D" <ndstepp at hrl.com>:
> What is a good way (or I guess any way) of transforming a pattern like the above to something in mercury?

That depends on what kind of data you want to assert.
The Mercury standard library has a whole bunch of modules
that each define a data structure, together with the usual operations
on that data structure: lists, sets, bags, maps, multi_maps, graphs, etc.

> A tiny example is something like this:
> 
> :- dynamic factA/1.
> :- dynamic factB/1.
> 
> p(A,B) :- factA(A), factB(B).
> 
> callP(Facts, ABs) :-
>     assertFacts(Facts),
>     findall(X, p(X,X), Xs),
>     retractFacts(Facts).

The most precise translation of this Prolog code into Mercury
would be three error messages: one each about ABs and Xs being singletons,
the third about the output argument of callP being left undefined :-)

> Where assertFacts/1 and retractFacts/1 do the obvious-sounding things. Facts might be something like [factA(x),factB(y),factA(y)], with answer Xs=[y].

The actual replacement would be something like:

set.list_to_set([x, y], SetA),
set.list_to_set([y], SetB),
set.intersect(SetA, SetB, SetAB)
set.to_sorted_list(SetAB, Result)

yielding Result = [y],

or, if it duplicates are allowed, the equivalent of the above
using bags.

Zoltan.


More information about the users mailing list