Relation doesn't perform `call()`

I’m using: SWI-Prolog version 8.0.3

I want the code to to show the fact1

But what I’m getting is false

My code looks like this:

% -*- Mode: Prolog -*-
:- use_module(library(apply)).

byte_nonzero(B, R) :-
    B > 0, R = true.

byte_nonzero_acc(B, Acc0, Acc) :-
    Acc0, byte_nonzero(B, Acc).

bytes_nonzero(B, R) :-
    foldl(byte_nonzero_acc, B, true, R).

rule_nonzero(_, B, _, R) :-
    bytes_nonzero(B, R).

some_fact(0, [10, 20, 30], "fact1").
some_fact(10, [10, 0, 30], "fact2").

some_relation(rule, A) :-
    some_fact(A, B, C),
    call(rule, A, B, C, true).

So if I load this test.pl in the toplevel with

?- consult('test.pl').
true.
?- some_fact(A, B, C), call(rule_nonzero, A, B, C, true).
A = 0,
B = [10, 20, 30].
C = "fact1".
?- some_relation(rule_nonzero, A).
false.

Why it shows false. while the relation is essentially the same as the query I typed, and it worked (some_fact(A, B, C), call(rule_nonzero, A, B, C, true).).

It fails because there is no fact ‘some_relation(rule_nonzero,A).’, As a useful tip, turning on tracing would help identifier this bug in your code. I suspect you wanted

some_relation(Rule, A) :-
    some_fact(A,B,C),
    call(Rule, A, B, C, true).

Indeed, it works. So stupid mistake, sorry. Thanks for the quick answer!

I’m trying to understand the intent of your code.

If you’re trying to determine if all the items in a list are greater than zero, then this will do it without foldl and an accumulator that returns true or false:

gt_zero(B) :- B > 0.
all_gt_zero(List) :- maplist(gt_zero, List).

If you need to set a value to true or false (but probably you don’t):

all_gt_zero(List, true) :-
    maplist(gt_zero, List), !.
all_gt_zero(_, false).

or

all_gt_zero(List, Result) :-
    maplist(gt_zero, List) -> Result = true ; Result = false.

In summary, consider using Prolog’s succeed/fail semantics as-is without trying to capture the result into a variable that’s true or false.

1 Like

Functional habits. Also I can’t use ! operator, because I try to make the code also work with ProbLog.