# Cplint/pita forall / findall bug

**URL:** <https://swi-prolog.discourse.group/t/cplint-pita-forall-findall-bug/5189>\
**Category:** Pack\
**Tags:** bug\
**Created:** [March 30, 2022, 8:27am UTC](https://swi-prolog.discourse.group/t/cplint-pita-forall-findall-bug/5189 "2022-03-30T08:27:08Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Frank\_Schwidom](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/frank_schwidom/32/7662_2.png) [@Frank\_Schwidom](https://swi-prolog.discourse.group/u/Frank_Schwidom)\
**Post date:** [March 30, 2022, 8:27am UTC](https://swi-prolog.discourse.group/t/cplint-pita-forall-findall-bug/5189/1 "2022-03-30T08:27:08Z")

</div>

```prolog
:- 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

```

---

<div class="post-metadata">

**Author:** ![friguzzi](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/friguzzi/32/7045_2.png) [@friguzzi](https://swi-prolog.discourse.group/u/friguzzi)\
**Post date:** [March 30, 2022, 1:52pm UTC](https://swi-prolog.discourse.group/t/cplint-pita-forall-findall-bug/5189/2 "2022-03-30T13:52:14Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Frank\_Schwidom](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/frank_schwidom/32/7662_2.png) [@Frank\_Schwidom](https://swi-prolog.discourse.group/u/Frank_Schwidom)\
**Post date:** [March 30, 2022, 2:32pm UTC](https://swi-prolog.discourse.group/t/cplint-pita-forall-findall-bug/5189/3 "2022-03-30T14:32:14Z")

</div>

> [@friguzzi](#):
>
> findall and forall are not supported by the probabilistic semantics

So I can just have this workaround:

```prolog

:- 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.

---

<div class="post-metadata">

**Author:** ![friguzzi](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/friguzzi/32/7045_2.png) [@friguzzi](https://swi-prolog.discourse.group/u/friguzzi)\
**Post date:** [March 30, 2022, 5:17pm UTC](https://swi-prolog.discourse.group/t/cplint-pita-forall-findall-bug/5189/4 "2022-03-30T17:17:13Z")

</div>

Yes you can but what is your use case?

---

<div class="post-metadata">

**Author:** ![Frank\_Schwidom](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/frank_schwidom/32/7662_2.png) [@Frank\_Schwidom](https://swi-prolog.discourse.group/u/Frank_Schwidom)\
**Post date:** [March 30, 2022, 7:33pm UTC](https://swi-prolog.discourse.group/t/cplint-pita-forall-findall-bug/5189/5 "2022-03-30T19:33:46Z")

</div>

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

For instance this:

```prolog

:- 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:

```prolog
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.

---

<div class="post-metadata">

**Author:** ![Frank\_Schwidom](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/frank_schwidom/32/7662_2.png) [@Frank\_Schwidom](https://swi-prolog.discourse.group/u/Frank_Schwidom)\
**Post date:** [March 30, 2022, 9:17pm UTC](https://swi-prolog.discourse.group/t/cplint-pita-forall-findall-bug/5189/6 "2022-03-30T21:17:46Z")

</div>

> [@friguzzi](#):
>
> Yes you can but what is your use case?

Now I have a working example:

```prolog

:- 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.

---

<div class="post-metadata">

**Author:** ![friguzzi](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/friguzzi/32/7045_2.png) [@friguzzi](https://swi-prolog.discourse.group/u/friguzzi)\
**Post date:** [March 31, 2022, 8:35am UTC](https://swi-prolog.discourse.group/t/cplint-pita-forall-findall-bug/5189/7 "2022-03-31T08:35:27Z")

</div>

> > ![](https://global.discourse-cdn.com/free1/uploads/swiprolog/original/2X/3/3a332e193defac4b86af14a6923225ff380696ea.png "Frank\_Schwidom") | [Frank\_Schwidom](https://swi-prolog.discourse.group/u/frank_schwidom)  
> > March 30 |
> > 
> > - | - |
> 
> > ![](https://global.discourse-cdn.com/free1/uploads/swiprolog/original/2X/d/d027fffd113beb3bf89254b51ff5f71d09894d88.png)friguzzi:
> > 
> > Yes you can but what is your use case?
> 
> Now I have a working example:
> 
> ```prolog
> 
> :- 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
