Stochastic memoization

These examples are from the slides of various tutorials on ProbLog, e.g., at IJCAI 2015

The following example illustrates ProbLog’s use of stochastic memoization. We first define a predicate p5 that for a number N given as its first argument, randomly sets its second argument M to either N+5 or N. We then apply this function to every element of a given list of numbers.

0.4::p5(N,N);0.6::p5(N,M) :- M is N+5. lp5([],[]). lp5([N|L],[M|K]) :- p5(N,M), lp5(L,K). query(lp5([1,2],_)). query(lp5([1,1],_)).

The version above reuses the previous, memoized, decision if it encounters the same number again. We can avoid this by adding a third argument to the basic function (sometimes called a “trial identifier”), and making sure that this argument differs at every call (here, by using the rest of the input list as identifier).

0.4::p5_id(N,N,ID);0.6::p5_id(N,M,ID) :- M is N+5. lp5_id([],[]). lp5_id([N|L],[M|K]) :- p5_id(N,M,L), lp5_id(L,K). query(lp5_id([1,1],_)). query(lp5_id([1,2],_)).