ERROR EXCEPTION while printing message raised: type_error on plunit run_tests - prolog

These are defined in chess.pl
:- dynamic drop/1.
:- dynamic start/1.
a_drop(X) :- piese(X), piese_pos(X, Y), \+ on(Y), assertz(drop(X)).
a_pickup(X) :- retract(drop(_-_-X)).
a_clear:- retract(drop(_)).
print_drops:- forall(drop(X), write(X)).
on(X) :- once(drop(_-_-X)).
off(X) :- \+ drop(_-_-X).
I have this test file:
:- begin_tests(chess).
:- include(chess).
clear_board :- a_clear.
board_1_setup :- a_drop(b-r-(b-6)), a_drop(w-k-(d-5)), a_drop(w-b-(c-4)), a_drop(b-b-(b-3)).
test(hello, [
setup(board_1_setup),
cleanup(clear_board)
]) :- on(b-4).
:- end_tests(chess).
When I run this command:
swipl -g run_tests -t halt _chess.plt
This is the output.
% PL-Unit: chess
ERROR:
[[ EXCEPTION while printing message url('/home/eguneys/chess/pro/_chess.plt':10)
with arguments []:
raised: type_error(text,url('/home/eguneys/chess/pro/_chess.plt':10))
]]
:
test hello: failed
done
% 1 test failed
% 0 tests passed
ERROR: -g run_tests: false
I expected the test to fail, but with a friendlier message, what is this ERROR garbage.
Please help.
Tests passes normal when I test for example: ... :- on(b-6).

This seems to be fixed in the development version (ppa:swi-prolog/devel).
There appear to be typos or things missing in your test files:
$ swipl -g run_tests -t halt chess.plt
% PL-Unit: chess
ERROR: /tmp/chess.plt:9:
error in setup: plunit_chess:a_drop/1: Unknown procedure: plunit_chess:piese/1
done
% No tests to run

Related

CHR in SWI Prolog: Rule guard containing "just removed" constraint does not return, blows stack

A little test code with no particular meaning:
:- use_module(library(chr)).
:- chr_constraint foo/1, bar/1, anyone/2.
foo(X) \ bar(Y) <=> anyone(X,Y).
anyone(_,Y) <=> bar(Y) | writef("bar(%w) exists\n",[Y]).
anyone(_,Y) <=> writef("bar(%w) does not exist\n",[Y]).
If the above is consulted from the SWI Prolog command line and then run with:
?- foo(8),bar(10).
there is overflow after some time:
Could not reenable global-stack
Could not reenable global-stack
ERROR: Out of global-stack.
ERROR: No room for exception term. Aborting.
Could not reenable global-stack
ERROR: Out of global-stack.
ERROR: No room for exception term. Aborting.
ERROR: Execution Aborted
% Execution Aborted
However, if the code for the second rule is changed by moving the test for bar from the guard ot the head:
:- use_module(library(chr)).
:- chr_constraint foo/1, bar/1, anyone/2.
foo(X) \ bar(Y) <=> anyone(X,Y).
anyone(_,Y),bar(Y) <=> writef("bar(%w) exists\n",[Y]). % changed!
anyone(_,Y) <=> writef("bar(%w) does not exist\n",[Y]).
then execution terminates:
?- foo(8),bar(10).
bar(10) does not exist
foo(8).
Is this an accident of the implementation? It does seem a bit dicey.
Furthermore, the above code was written because I wanted to test whether the constraint to be removed from the store (here, bar(Y)) would be available in the body of the rule. Apparently not, it is already gone when the rule body executes; but is this implementation dependent or a necessary or specified behaviour of CHR?

operator expected in prolog while reading a file

I am getting an error when i tried to read a file as operator expected.
main:-
open('cr1.txt',read,Str),
read_houses(Str,Houses),
close(Str),
write(Houses), nl.
read_houses(Stream,[]):-
at_end_of_stream(Stream).
read_houses(Stream,[X|L]):-
\+ at_end_of_stream(Stream),
read(Stream,X),
read_houses(Stream,L).

Existence error on lists

I am using gprolog allong with gplc and I get fatal error: redefining built-in predicate member/2 when I try to compile the following code. I also get uncaught exception: error(existence_error(procedure,member/0),member/0) when I try to consult the same code.
Code:
member(X,[X|T]).
member(X,[H|T]) :- member(X,T).
The query I give:
| ?- member(X,[a,b,c,d,e]).
X = a ? ;
uncaught exception: error(existence_error(procedure,member/0),member/0)

SWI-Prolog ERROR: No permission to open websocket

I tried the code below and I got:
ERROR: No permission to open websocket `'ws://localhost:9999''.
Why?
If I use root(.), it's ok.
UPDATED (correct code):
:- use_module(library(http/websocket)).
:- use_module(library(http/thread_httpd)).
:- use_module(library(http/http_dispatch)).
:- use_module(library(http/http_path)).
:- use_module(library(http/http_authenticate)).
:- initialization main.
main :-
run.
:- http_handler(root(ws), http_upgrade_to_websocket(echo, []), [spawn([])]).
echo(WebSocket) :-
ws_receive(WebSocket, Message),
( Message.opcode == close
-> true
; ws_send(WebSocket, Message),
echo(WebSocket)
).
run :-
run(9999).
run(Port) :-
http_server(http_dispatch, [port(Port)]).
stop :-
stop(9999).
stop(Port) :-
http_stop_server(Port, []).
In abstract terms, If you use root(X), then this means that the path where this is available is /X.
So, if you specify root(ws), then in your case, you would have to access:
ws://localhost:9999/ws
If you specify root(.) then / suffices etc.
Next time, please specify a complete example, so that others can actually try out your code without having to guess the remainder of your snippet.

Raise an error in SWI Prolog

I'd like to print a message and stop the evaluation of the predicate. How do I do that?
Take a look at this link where the catch/3 and throw/1 mechanisms in Prolog are described.
It is possible to throw an exception or handle an exception using this mechanism.
The example (given on the site) is:
p:- true.
p:- throw(b).
q:- catch(p, B, write('hellop')), r(c).
r(X) :- throw(X).
Then the call:
?- catch(p, X, (write('error from p'), nl)).
will illustrate the ecxeption handling.
I played with a few other examples which I found. This might be usefull.
p :- throw(b).
r(X) :- throw(X).
q:- catch(p, B, format('Output1: Error from throwing p')), r(B).
catch(throw(exit(1)), exit(X), format('Output: `w', [X])).
Output: 1
1 is thrown to catcher 'exit(X)' and recovered by format('Output: ~w', [X])),
catch(p, C, format('hellop')).
Output: hellop
p throws 'b' which is recovered by writing 'hellop'
catch(q, C, format('Output2, Recovering q: helloq ')).
Output1: Error from throwing p
Output2, Recovering q: helloq
Ben
The stderr stream is aliased to user_error (see https://www.swi-prolog.org/pldoc/man?section=streamalias )
You can write a term (like a string) to a stream using write/2.
You can stop the program using halt.
For example:
% Happy case. Comes first, uses ! to avoid taking the alternative branch
foo(X) :- bar(X, Y), baz(Y), !.
% Fallback branch, in case the above failed. Writes a message and halts.
foo(X) :-
write(user_error, "Couldn't foo"),
halt.

Resources