Cplint/pita forall / findall bug

:- use_module( library( pita)).

:- pita.

:- begin_lpad.

a.

b :- forall(prob( a, _), true). % don't work

c :- findall(1, prob( a, _), _). % don't work

d :- prob( a, _). % works

e :- forall(true, true). % works

f :- findall(1, true, _). % works

:- end_lpad.

/*
(ins)?- prob(b,P).
P = 0.0. % wrong

(cmd)?- prob(c,P).
P = 0.0. % wrong

(ins)?- prob(d,P).
P = 1.0. % ok

(cmd)?- prob(e,P).
P = 1.0. % ok

(ins)?- prob(f,P).
P = 1.0. % ok

*/

Regards

Hi Frank,
findall and forall are not supported by the probabilistic semantics. They are considered as builtin predicates and evaluated as they are, so if the query involves a probabilistic atom or the meta predicate prob it doesn’t work.

Best
Fabrizio

2 Likes

So I can just have this workaround:


:- use_module( library( pita)).

:- pita.

:- dynamic data/1.

a :- prob( a, _).

b.

e :- abolish(data/1), forall( prob( a, P), assertz( data(P))).

:- begin_lpad.

a.

b :- forall( prob(a,_), true). % don't work
c :- forall( a, true). % don't work
d :- forall( b, true). % works

% e :- db(e). % no P possible

f :- forall( data(_), true).

:- end_lpad.

/*
(ins)?- e, prob(f, P).
P = 1.0.
*/

Regards.

Yes you can but what is your use case?

I have no concrete usecase, just playing around and learning.

For instance this:


:- begin_lpad.
b(x) : 0.6; b(y) : 0.4.

a : P2 :- findall( X-P, prob( b(X), P), L), postprocess(L, P2).
:- end_lpad.

I want to gather probabilities of a predicate (b) in a list and postprocess them before I set a new probability for the predicate (a).

but maybe I should write something like:

b(x,0.6).
b(y,0.4).

:- begin_lpad.
b(X) : P :- db( b(X,P)).

a : P2 :- findall( X-P, b(X, P), L), postprocess(L, P2).

:- end_lpad.

This should work.

Now I have a working example:


:- use_module( library(pita)).

:- pita.

b(x,0.6).
b(y,0.4).

:- begin_lpad.

b : P :- db( b(X,P)).
b(X) : P :- db( b(X,P)).

c :- b(X1), b(X2), X1 \== X2.

postprocess( []).
postprocess( L) : P:- true
 , L = [_X-P|T] 
 , postprocess(T)
 .

a :- findall( X-P, b(X, P), L), postprocess(L).

:- end_lpad.


/*

(ins)?- prob( a, P).
P = 0.24.

(cmd)?- prob( c, P).
P = 0.24.

(ins)?- prob( b, P).
P = 0.76.

(ins)?- prob( b(X), P).
X = x,
P = 0.6 ;
X = y,
P = 0.4.


*/

a does the same as c but a is more flexible. So I could extend b/2 on top and a would be up to date.
c in the other hand should be updated as well on every step to work correctly.

And maybe I would be able to derive the last b/2 from al other b/2’s so that the P sum is always 1.

But I encountered a little trap. I made a mistake by writing ‘b(X) : P : db( b(X,P)).’ instead of ‘b(X) : P :- db( b(X,P)).’ and I didn’t see the mistake. And I wondered why it does not work. Next time I will look if this syntactic variation can be used for something useful.

Regards.

Frank_Schwidom
March 30

friguzzi:

Yes you can but what is your use case?

Now I have a working example:


:- use_module( library(pita)).

:- pita.

b(x,0.6).

b(y,0.4).

:- begin_lpad.

b : P :- db( b(X,P)).

b(X) : P :- db( b(X,P)).

c :- b(X1), b(X2), X1 \== X2.

postprocess( []).

postprocess( L) : P:- true

 , L = [_X-P|T] 

 , postprocess(T)

 .

a :- findall( X-P, b(X, P), L), postprocess(L).

:- end_lpad.

/*

(ins)?- prob( a, P).

P = 0.24.

(cmd)?- prob( c, P).

P = 0.24.

(ins)?- prob( b, P).

P = 0.76.

(ins)?- prob( b(X), P).

X = x,

P = 0.6 ;

X = y,

P = 0.4.

*/

a does the same as c but a is more flexible. So I could extend b/2 on top and a would be up to date.
c in the other hand should be updated as well on every step to work correctly.

And maybe I would be able to derive the last b/2 from al other b/2’s so that the P sum is always 1.

But I encountered a little trap. I made a mistake by writing ‘b(X) : P : db( b(X,P)).’ instead of ‘b(X) : P :- db( b(X,P)).’ and I didn’t see the mistake. And I wondered why it does not work. Next time I will look if this syntactic variation can be used for something useful.

I believe b(X) : P : db( b(X,P)) will be interpreted as a fact, not as a probabilistic fact, that’s why it didn’t work

Fabrizio