Different behaviour when loading from the command line and when loading from prompt

Hi,
in cplint, If I load file test.pl in prolog/cplint_test with

swipl test.pl
and run

run_tests([dt_viral]).
I get an error which I don’t get if I consult test from the prompt

[test].
run_tests([dt_viral]).

How is it possible?

Fabrizio

And the error you get is…?

1 Like

That doesn’t look right.

Should it not be run_tests. or run_tests(dt_viral).

run_tests/1

Run only the specified tests. Spec can be a list to run multiple tests. A single specification is either the name of a test unit or a term <Unit>:<Tests>, running only the specified test. <Tests> is either the name of a test or a list of names. Running particular tests is particularly useful for tracing a test:

Thanks Eric and Paulo.
I just noticed the two ways of loading the program result in different programs in memory.

for swipl test.pl I get

[1]  ?- listing(plunit_dt_viral:_).

:- dynamic'__wrap$trusts'/4.
:- volatile'__wrap$trusts'/4.

'__wrap$trusts'(B, C, D, A) :-
    (   var(A)
    ->  true
    ;   '$uninstantiation_error'(A)
    ),
    start_tabling(plunit_dt_viral:trusts(B, C, D, A),
                  call(<closure>(plunit_dt_viral:trusts/4)(B,
                                                           C,
                                                           D,
                                                           A)),
                  plunit_dt_viral:'$trusts$1'(B, C, D),
                  A).

:- dynamic query_rule/4.


buys(B, A, G) :-
    onec(A, C),
    marketed(B, A, D),
    andc(A, C, D, E),
    buy_from_marketing(B, A, F),
    andc(A, E, F, G).
buys(B, A, J) :-
    onec(A, C),
    trusts(B, E, A, D),
    andc(A, C, D, F),
    buy_from_trust(B, E, A, G),
    andc(A, F, G, H),
    buys(E, A, I),
    andc(A, H, I, J).

:- dynamic'__wrap$buy_from_trust'/4.
:- volatile'__wrap$buy_from_trust'/4.

'__wrap$buy_from_trust'(B, C, D, A) :-
    (   var(A)
    ->  true
    ;   '$uninstantiation_error'(A)
    ),
    start_tabling(plunit_dt_viral:buy_from_trust(B, C, D, A),
                  call(<closure>(plunit_dt_viral:buy_from_trust/4)(B,
                                                                   C,
                                                                   D,
                                                                   A)),
                  plunit_dt_viral:'$buy_from_trust$1'(B, C, D),
                  A).

buy_from_marketing(B, A, D) :-
    get_var_n(plunit_dt_viral, A, 8, [B], [0.2, 0.8], C),
    equalityc(A, C, 0, D).

:- dynamic av/3.


trusts(B, C, A, F) :-
    onec(A, D),
    trusts_directed(B, C, A, E),
    andc(A, D, E, F).
trusts(C, B, A, F) :-
    onec(A, D),
    trusts_directed(B, C, A, E),
    andc(A, D, E, F).

:- dynamic pita_on/0.


:- dynamic zero_clauses/1.

zero_clauses((marketed(A, B, C):-maplist(nonvar, [A]), zeroc(B, C))).
zero_clauses((buy_from_marketing(A, B, C):-maplist(nonvar, [A]), zeroc(B, C))).
zero_clauses((buy_from_trust(A, B, C, D):-maplist(nonvar, [A, B]), zeroc(C, D))).
zero_clauses((trusts(A, B, C, D):-maplist(nonvar, [A, B]), zeroc(C, D))).
zero_clauses((trusts_directed(A, B, C, D):-maplist(nonvar, [A, B]), zeroc(C, D))).
zero_clauses((buys(A, B, C):-maplist(nonvar, [A]), zeroc(B, C))).

buy_from_trust(B, C, A, E) :-
    get_var_n(plunit_dt_viral, A, 9, [B, C], [0.3, 0.7], D),
    equalityc(A, D, 0, E).

:- dynamic'__wrap$buy_from_marketing'/3.
:- volatile'__wrap$buy_from_marketing'/3.

'__wrap$buy_from_marketing'(B, C, A) :-
    (   var(A)
    ->  true
    ;   '$uninstantiation_error'(A)
    ),
    start_tabling(plunit_dt_viral:buy_from_marketing(B, C, A),
                  call(<closure>(plunit_dt_viral:buy_from_marketing/3)(B,
                                                                       C,
                                                                       A)),
                  plunit_dt_viral:'$buy_from_marketing$1'(B, C),
                  A).

marketed(theo, A, C) :-
    get_dec_var_n(plunit_dt_viral, A, 0, [], B),
    equalityc(A, B, 0, C).
marketed(bernd, A, C) :-
    get_dec_var_n(plunit_dt_viral, A, 1, [], B),
    equalityc(A, B, 0, C).
marketed(guy, A, C) :-
    get_dec_var_n(plunit_dt_viral, A, 2, [], B),
    equalityc(A, B, 0, C).
marketed(ingo, A, C) :-
    get_dec_var_n(plunit_dt_viral, A, 3, [], B),
    equalityc(A, B, 0, C).
marketed(angelika, A, C) :-
    get_dec_var_n(plunit_dt_viral, A, 4, [], B),
    equalityc(A, B, 0, C).
marketed(martijn, A, C) :-
    get_dec_var_n(plunit_dt_viral, A, 5, [], B),
    equalityc(A, B, 0, C).
marketed(laura, A, C) :-
    get_dec_var_n(plunit_dt_viral, A, 6, [], B),
    equalityc(A, B, 0, C).
marketed(kurt, A, C) :-
    get_dec_var_n(plunit_dt_viral, A, 7, [], B),
    equalityc(A, B, 0, C).

:- dynamic v/3.


:- dynamic dec/3.


'unit test'(best_st_viral, 72, [true(true)], plunit_dt_viral:'unit body'('best_st_viral@line 72', vars)).

:- dynamic rule_n/1.

rule_n(10).

:- dynamic tabled/1.

tabled(marketed(_)).
tabled(buy_from_marketing(_)).
tabled(buy_from_trust(_, _)).
tabled(trusts(_, _)).
tabled(trusts_directed(_, _)).
tabled(buys(_)).

'unit body'('best_st_viral@line 72', vars) :-
    !,
    ansi_format([bold, fg(cyan)], '~nThis test takes few seconds.~n', []),
    run((dt_solve(Strategy, ExpValue), close_to(ExpValue, 3.21), perm(Strategy, [[marketed(theo)], [marketed(martijn)], [marketed(ingo)], [marketed(guy)]]))).

trusts_directed(bernd, ingo, A, B) :-
    onec(A, B).
trusts_directed(ingo, theo, A, B) :-
    onec(A, B).
trusts_directed(theo, angelika, A, B) :-
    onec(A, B).
trusts_directed(bernd, martijn, A, B) :-
    onec(A, B).
trusts_directed(ingo, martijn, A, B) :-
    onec(A, B).
trusts_directed(martijn, guy, A, B) :-
    onec(A, B).
trusts_directed(guy, theo, A, B) :-
    onec(A, B).
trusts_directed(guy, angelika, A, B) :-
    onec(A, B).
trusts_directed(laura, ingo, A, B) :-
    onec(A, B).
trusts_directed(laura, theo, A, B) :-
    onec(A, B).
trusts_directed(laura, guy, A, B) :-
    onec(A, B).
trusts_directed(laura, martijn, A, B) :-
    onec(A, B).
trusts_directed(kurt, bernd, A, B) :-
    onec(A, B).

:- dynamic rule_by_num/4.

rule_by_num(0, [marketed(theo)], [], []).
rule_by_num(1, [marketed(bernd)], [], []).
rule_by_num(2, [marketed(guy)], [], []).
rule_by_num(3, [marketed(ingo)], [], []).
rule_by_num(4, [marketed(angelika)], [], []).
rule_by_num(5, [marketed(martijn)], [], []).
rule_by_num(6, [marketed(laura)], [], []).
rule_by_num(7, [marketed(kurt)], [], []).
rule_by_num(8, [buy_from_marketing(A):0.2, '':0.8], [], [A]).
rule_by_num(9, [buy_from_trust(A, B):0.3, '':0.7], [], [A, B]).

:- dynamic goal_n/1.

goal_n(0).

:- dynamic'__wrap$marketed'/3.
:- volatile'__wrap$marketed'/3.

'__wrap$marketed'(B, C, A) :-
    (   var(A)
    ->  true
    ;   '$uninstantiation_error'(A)
    ),
    start_tabling(plunit_dt_viral:marketed(B, C, A),
                  call(<closure>(plunit_dt_viral:marketed/3)(B,
                                                             C,
                                                             A)),
                  plunit_dt_viral:'$marketed$1'(B, C),
                  A).

:- dynamic'__wrap$trusts_directed'/4.
:- volatile'__wrap$trusts_directed'/4.

'__wrap$trusts_directed'(B, C, D, A) :-
    (   var(A)
    ->  true
    ;   '$uninstantiation_error'(A)
    ),
    start_tabling(plunit_dt_viral:trusts_directed(B, C, D, A),
                  call(<closure>(plunit_dt_viral:trusts_directed/4)(B,
                                                                    C,
                                                                    D,
                                                                    A)),
                  plunit_dt_viral:'$trusts_directed$1'(B, C, D),
                  A).

:- dynamic local_pita_setting/2.

local_pita_setting(epsilon_parsing, 1.0e-5).
local_pita_setting(bagof, false).
local_pita_setting(compiling, off).
local_pita_setting(depth_bound, false).
local_pita_setting(depth, 5).
local_pita_setting(single_var, false).
local_pita_setting(tabling, auto).
local_pita_setting(prism_memoization, false).

:- dynamic'__wrap$buys'/3.
:- volatile'__wrap$buys'/3.

'__wrap$buys'(B, C, A) :-
    (   var(A)
    ->  true
    ;   '$uninstantiation_error'(A)
    ),
    start_tabling(plunit_dt_viral:buys(B, C, A),
                  call(<closure>(plunit_dt_viral:buys/3)(B,
                                                         C,
                                                         A)),
                  plunit_dt_viral:'$buys$1'(B, C),
                  A).
true.

for

swipl
?- [test].

I get

?- listing(plunit_dt_viral:_).

:- dynamic'__wrap$buy_from_trust'/4.
:- volatile'__wrap$buy_from_trust'/4.

'__wrap$buy_from_trust'(B, C, D, A) :-
    (   var(A)
    ->  true
    ;   '$uninstantiation_error'(A)
    ),
    start_tabling(plunit_dt_viral:buy_from_trust(B, C, D, A),
                  call(<closure>(plunit_dt_viral:buy_from_trust/4)(B,
                                                                   C,
                                                                   D,
                                                                   A)),
                  plunit_dt_viral:'$buy_from_trust$1'(B, C, D),
                  A).

:- dynamic query_rule/4.


'unit body'('best_st_viral@line 72', vars) :-
    !,
    ansi_format([bold, fg(cyan)], '~nThis test takes few seconds.~n', []),
    run((dt_solve(Strategy, ExpValue), close_to(ExpValue, 3.21), perm(Strategy, [[marketed(theo)], [marketed(martijn)], [marketed(ingo)], [marketed(guy)]]))).

buys(B, A, G) :-
    onec(A, C),
    marketed(B, A, D),
    andc(A, C, D, E),
    buy_from_marketing(B, A, F),
    andc(A, E, F, G).
buys(B, A, J) :-
    onec(A, C),
    trusts(B, E, A, D),
    andc(A, C, D, F),
    buy_from_trust(B, E, A, G),
    andc(A, F, G, H),
    buys(E, A, I),
    andc(A, H, I, J).

:- dynamic'__wrap$buy_from_marketing'/3.
:- volatile'__wrap$buy_from_marketing'/3.

'__wrap$buy_from_marketing'(B, C, A) :-
    (   var(A)
    ->  true
    ;   '$uninstantiation_error'(A)
    ),
    start_tabling(plunit_dt_viral:buy_from_marketing(B, C, A),
                  call(<closure>(plunit_dt_viral:buy_from_marketing/3)(B,
                                                                       C,
                                                                       A)),
                  plunit_dt_viral:'$buy_from_marketing$1'(B, C),
                  A).

:- dynamic av/3.


marketed(theo, A, C) :-
    get_dec_var_n(plunit_dt_viral, A, 0, [], B),
    equalityc(A, B, 0, C).
marketed(bernd, A, C) :-
    get_dec_var_n(plunit_dt_viral, A, 1, [], B),
    equalityc(A, B, 0, C).
marketed(guy, A, C) :-
    get_dec_var_n(plunit_dt_viral, A, 2, [], B),
    equalityc(A, B, 0, C).
marketed(ingo, A, C) :-
    get_dec_var_n(plunit_dt_viral, A, 3, [], B),
    equalityc(A, B, 0, C).
marketed(angelika, A, C) :-
    get_dec_var_n(plunit_dt_viral, A, 4, [], B),
    equalityc(A, B, 0, C).
marketed(martijn, A, C) :-
    get_dec_var_n(plunit_dt_viral, A, 5, [], B),
    equalityc(A, B, 0, C).
marketed(laura, A, C) :-
    get_dec_var_n(plunit_dt_viral, A, 6, [], B),
    equalityc(A, B, 0, C).
marketed(kurt, A, C) :-
    get_dec_var_n(plunit_dt_viral, A, 7, [], B),
    equalityc(A, B, 0, C).

:- dynamic'__wrap$trusts'/4.
:- volatile'__wrap$trusts'/4.

'__wrap$trusts'(B, C, D, A) :-
    (   var(A)
    ->  true
    ;   '$uninstantiation_error'(A)
    ),
    start_tabling(plunit_dt_viral:trusts(B, C, D, A),
                  call(<closure>(plunit_dt_viral:trusts/4)(B,
                                                           C,
                                                           D,
                                                           A)),
                  plunit_dt_viral:'$trusts$1'(B, C, D),
                  A).

:- dynamic'__wrap$buys'/3.
:- volatile'__wrap$buys'/3.

'__wrap$buys'(B, C, A) :-
    (   var(A)
    ->  true
    ;   '$uninstantiation_error'(A)
    ),
    start_tabling(plunit_dt_viral:buys(B, C, A),
                  call(<closure>(plunit_dt_viral:buys/3)(B,
                                                         C,
                                                         A)),
                  plunit_dt_viral:'$buys$1'(B, C),
                  A).

:- dynamic'__wrap$trusts_directed'/4.
:- volatile'__wrap$trusts_directed'/4.

'__wrap$trusts_directed'(B, C, D, A) :-
    (   var(A)
    ->  true
    ;   '$uninstantiation_error'(A)
    ),
    start_tabling(plunit_dt_viral:trusts_directed(B, C, D, A),
                  call(<closure>(plunit_dt_viral:trusts_directed/4)(B,
                                                                    C,
                                                                    D,
                                                                    A)),
                  plunit_dt_viral:'$trusts_directed$1'(B, C, D),
                  A).

:- dynamic'__wrap$marketed'/3.
:- volatile'__wrap$marketed'/3.

'__wrap$marketed'(B, C, A) :-
    (   var(A)
    ->  true
    ;   '$uninstantiation_error'(A)
    ),
    start_tabling(plunit_dt_viral:marketed(B, C, A),
                  call(<closure>(plunit_dt_viral:marketed/3)(B,
                                                             C,
                                                             A)),
                  plunit_dt_viral:'$marketed$1'(B, C),
                  A).

:- dynamic v/3.


:- dynamic rule_n/1.

rule_n(10).

:- dynamic dec/3.


trusts_directed(bernd, ingo, A, B) :-
    onec(A, B).
trusts_directed(ingo, theo, A, B) :-
    onec(A, B).
trusts_directed(theo, angelika, A, B) :-
    onec(A, B).
trusts_directed(bernd, martijn, A, B) :-
    onec(A, B).
trusts_directed(ingo, martijn, A, B) :-
    onec(A, B).
trusts_directed(martijn, guy, A, B) :-
    onec(A, B).
trusts_directed(guy, theo, A, B) :-
    onec(A, B).
trusts_directed(guy, angelika, A, B) :-
    onec(A, B).
trusts_directed(laura, ingo, A, B) :-
    onec(A, B).
trusts_directed(laura, theo, A, B) :-
    onec(A, B).
trusts_directed(laura, guy, A, B) :-
    onec(A, B).
trusts_directed(laura, martijn, A, B) :-
    onec(A, B).
trusts_directed(kurt, bernd, A, B) :-
    onec(A, B).

:- dynamic rule_by_num/4.

rule_by_num(0, [marketed(theo)], [], []).
rule_by_num(1, [marketed(bernd)], [], []).
rule_by_num(2, [marketed(guy)], [], []).
rule_by_num(3, [marketed(ingo)], [], []).
rule_by_num(4, [marketed(angelika)], [], []).
rule_by_num(5, [marketed(martijn)], [], []).
rule_by_num(6, [marketed(laura)], [], []).
rule_by_num(7, [marketed(kurt)], [], []).
rule_by_num(8, [buy_from_marketing(A):0.2, '':0.8], [], [A]).
rule_by_num(9, [buy_from_trust(A, B):0.3, '':0.7], [], [A, B]).

:- dynamic tabled/1.

tabled(marketed(_)).
tabled(buy_from_marketing(_)).
tabled(buy_from_trust(_, _)).
tabled(trusts(_, _)).
tabled(trusts_directed(_, _)).
tabled(buys(_)).

'unit test'(best_st_viral, 72, [true(true)], plunit_dt_viral:'unit body'('best_st_viral@line 72', vars)).

:- dynamic goal_n/1.

goal_n(0).

:- dynamic pita_on/0.


:- dynamic zero_clauses/1.

zero_clauses((marketed(A, B, C):-maplist(nonvar, [A]), zeroc(B, C))).
zero_clauses((buy_from_marketing(A, B, C):-maplist(nonvar, [A]), zeroc(B, C))).
zero_clauses((buy_from_trust(A, B, C, D):-maplist(nonvar, [A, B]), zeroc(C, D))).
zero_clauses((trusts(A, B, C, D):-maplist(nonvar, [A, B]), zeroc(C, D))).
zero_clauses((trusts_directed(A, B, C, D):-maplist(nonvar, [A, B]), zeroc(C, D))).
zero_clauses((buys(A, B, C):-maplist(nonvar, [A]), zeroc(B, C))).

buy_from_trust(B, C, A, E) :-
    get_var_n(plunit_dt_viral, A, 9, [B, C], [0.3, 0.7], D),
    equalityc(A, D, 0, E).

:- dynamic local_pita_setting/2.

local_pita_setting(epsilon_parsing, 1.0e-5).
local_pita_setting(bagof, false).
local_pita_setting(compiling, off).
local_pita_setting(depth_bound, false).
local_pita_setting(depth, 5).
local_pita_setting(single_var, false).
local_pita_setting(tabling, auto).
local_pita_setting(prism_memoization, false).

buy_from_marketing(B, A, D) :-
    get_var_n(plunit_dt_viral, A, 8, [B], [0.2, 0.8], C),
    equalityc(A, C, 0, D).

trusts(B, C, A, F) :-
    onec(A, D),
    trusts_directed(B, C, A, E),
    andc(A, D, E, F).
trusts(C, B, A, F) :-
    onec(A, D),
    trusts_directed(B, C, A, E),
    andc(A, D, E, F).
true.

The tested program is

:- use_module(library(pita)).

:- pita.

:- begin_lpad.

?:: marketed(theo).

?:: marketed(bernd).

?:: marketed(guy).

?:: marketed(ingo).

?:: marketed(angelika).

?:: marketed(martijn).

?:: marketed(laura).

?:: marketed(kurt).

utility(marketed(theo),-2).

utility(marketed(bernd),-2).

utility(marketed(guy),-2).

utility(marketed(ingo),-2).

utility(marketed(angelika),-2).

utility(marketed(martijn),-2).

utility(marketed(laura),-2).

utility(marketed(kurt),-2).

utility(buys(theo),5).

utility(buys(bernd),5).

utility(buys(guy),5).

utility(buys(ingo),5).

utility(buys(angelika),5).

utility(buys(martijn),5).

utility(buys(laura),5).

utility(buys(kurt),5).

0.2 :: buy_from_marketing(_).

0.3 :: buy_from_trust(_,_).

trusts(X,Y) :- trusts_directed(X,Y).

trusts(X,Y) :- trusts_directed(Y,X).

trusts_directed(bernd,ingo).

trusts_directed(ingo,theo).

trusts_directed(theo,angelika).

trusts_directed(bernd,martijn).

trusts_directed(ingo,martijn).

trusts_directed(martijn,guy).

trusts_directed(guy,theo).

trusts_directed(guy,angelika).

trusts_directed(laura,ingo).

trusts_directed(laura,theo).

trusts_directed(laura,guy).

trusts_directed(laura,martijn).

trusts_directed(kurt,bernd).

buys(X):-

marketed(X),

buy_from_marketing(X).

buys(X):-

trusts(X,Y),

buy_from_trust(X,Y),

buys(Y).

:- end_lpad.

/*

* ?- dt_solve(Strategy,Value).

* Expected result:

* Strategy = [[marketed(theo)],[marketed(martijn)],[marketed(ingo)],[marketed(guy)]]

* Value = 3.21

*/

The two listing seem to contain the same code after close inspection. I don’t understand the difference in the order of clauses

The difference does not appear under macos

When listing all predicates from a module it gets the predicates from the module’s predicate hash table and as the hashes are (if I recall correctly) based on the functor (name/arity pair) handle, the order is next to random (between runs; in the same Prolog process they only change on a re-hash). The clauses of each predicate should be in database order.

It might make sense to list predicates either in (say) standard order of terms or ordered by source line number (although they may come from different files, so this may get hard). Using the source order might come more natural. Using standard order makes it easier to compare programs that are (for example) generated in different ways.

Which difference? Listing or error vs. no error? And there is still the open question “which error”. In theory the result should be the same, unless the result depends on e.g. order of current_predicate/1 as we have seen with listing/1.

I meant the different result of the unit test. It appears under linux and mac, we are trying to track down the issue.
Basically in cplint the file test.pl loads the tests for the individual models: if I load the individual files the testing succeeds, if I use test.pl I get an error for a test on one of the files. This also depends on how I load test.pl: if from the command line such as
swipl test.plor from the prompt with [test]. In the latter case testing succeeds.

Smells fishy. As cplint is using tabling, one should be aware that tabled results are returned in hash-table order. There could be bugs in the tabling implementation or there could be something in your test code that is order dependent (possibly checking for the correct result :slight_smile: