Reputation: 4477
When you execute anything in prolog you can see that the result of the evaluation comes out as:
true
or
false
I would like to change those values for personalized ones.
Based on this question, I saw that what I want is defined in the messages.pl
file
where I found that they are defined as:
query_result(no) --> % failure
[ ansi(truth(false), 'false.', []) ],
extra_line.
query_result(yes(true, [])) --> % prompt_alternatives_on: groundness
!,
[ ansi(truth(true), 'true.', []) ],
extra_line.
I'd like that instead of getting false
or true
, I could get <ERROR::>
and <PASSED::>
respectively for my unit tests.
I am doing the unit test to a file called adition.pl
consisting solely of.
my_add(A,B,Result):- number(A), number(B), is(Result,+(A,B)).
:-['C:/Users/RuslanLopez/Documents/Prolog/adittion.pl'].
%:-['C:/Program Files/swipl/boot/messages.pl'].
:- begin_tests(my_add).
:- include(adittion).
%:- use_module($messages).
%:- include(messages).
error:-write('<ERROR::>'),nl.
passed:-write('<PASSED::>'),nl.
:- dynamic(user:query_result/1).
user:query_result(no) --> % failure
[ ansi(truth(false),'<ERROR::>', []) ].
user:query_result(yes(true, [])) --> % prompt_alternatives_on: groundness
!,
[ ansi(truth(true),'<PASSED::>' , []) ].
test(my_add):-
my_add(1,2,Result),
Result =:= 3.
test(my_add) :-
my_add(1,2,Result),
Result \= 4.
:- end_tests(my_add).
I understand that the more straightforward solution would be to go to the file and change the value directly there, but I really wish I could make the change at runtime to change that custom behavior only in my unit test and not for the entire system.
Upvotes: 0
Views: 845
Reputation:
The toplevel is just a predicate prolog/0
that is run when the Prolog interpreter is started. Command line options allow running another predicate or then you need to modified the existing is code, if you want a different toplevel by default.
I got an open source top-level in my Prolog system fully written in Prolog itself. It can also show solved constraints. Its open source here:
Open Source: Module "session"
runtime/session.p
The true and false values are internationalized through property files. So in German you see "Ja" and "Nein", and in English you see "Yes" and "No". Since internationalization API is not really standardized you find some custom built-in calls.
A properties file needs to be preloaded via this statement here:
:- sys_load_resource(runtime).
And a value for a key, depending on current language settings, is then retrieve via such a code sequence:
sys_show_assoc([], _) :-
get_properties(runtime, P),
get_property(P, 'query.yes', V),
write(V).
Upvotes: 1
Reputation: 10102
Don't. You want to change a very central part of a Prolog system, namely the toplevel (loop). But you will need this toplevel to interactively diagnose and debug your program. Any change to the toplevel will be visible in that context too. The toplevel is the result of many decades of Prolog use by various user groups. It contains much more than the response true
and false
. Just try X = 1
, or even X = s(s(X))
, or dif(X,2)
.
plunit
is a unit testing package availabe for SWI, SICStus and some further systems. Use this instead, or roll our own, based on inspired by it.
Upvotes: 3