Implication branch doesn't see variables

52 views Asked by At

Here is a little test:

:- begin_tests(tuturuu).

test(foo) :- Expected=[a,b],Got=[a,b,c],verdict(foo,Expected,Got).

% helper: transform lists A,B into ordered sets As,Bs

setify(A,B,As,Bs) :-      % A,B -> As,Bs
   list_to_ord_set(A,As), % A->As
   list_to_ord_set(B,Bs). % B->Bs

% did the test pass? set-compare Expected and Got

verdict(Value,Expected,Got) :-   % V,E,G -> b?
   setify(Expected,Got,Eos,Gos),  
   format("For ~w, expected ~w and got ~w\n",[Value,Eos,Gos]),
   ord_seteq(Eos,Gos)
   -> true ; (format("For ~w, expected ~w but got ~w\n",[Value,Eos,Gos]),fail).

:- end_tests(tuturuu).


rt :- run_tests(tuturuu).

When I load this into SWI-Prolog:

?- [foo].
Warning: /home/somone/foo.pl:13:
Warning:    Singleton variable in branch: Eos
Warning:    Singleton variable in branch: Gos
true.

The -> branch doesn't have access to the (local) variables Eos and Gos? But why?

It has access to Value because that one appears in the head I suppose.

And indeed:

?- rt.
% PL-Unit: tuturuu For foo, expected [a,b] and got [a,b,c]
For foo, expected _29026 but got _29032                       <--- YUP NO ACCESS
ERROR: /home/someone/foo.pl:3:
        test foo: failed

 done
% 1 test failed
% 0 tests passed
false.

It don't really see why this restriction occurs, expect that maybe it's a part that hasn't been implemented in the compiler?

(Btw, we need one of those "keep the bar in the green windows" for unit testing from the Java World, like this: (stolen from here)

junit test bar

1

There are 1 answers

0
Paulo Moura On BEST ANSWER

Missing parenthesis? Try:

verdict(Value,Expected,Got) :-   % V,E,G -> b?
    setify(Expected,Got,Eos,Gos),  
    (   ord_seteq(Eos,Gos)
    ->  true
    ;   format("For ~w, expected ~w but got ~w\n",[Value,Eos,Gos]),
        fail
    ).

Note that the standard operator definitions are:

?- current_op(Priority, Type, ',').
Priority = 1000,
Type = xfy.

?- current_op(Priority, Type, '->').
Priority = 1050,
Type = xfy.

W.r.t. "keep the bar in the green windows", you can do it easily with Logtalk lgtunit tool (which you can also use to test Prolog code). E.g. https://logtalk.org/files/blog/2019_11_06/xunit_report.html See https://logtalk.org/2019/11/06/testing-multiple-implementations-of-a-protocol.html and https://logtalk.org/2019/12/02/generating-code-coverage-reports.html for an introduction.