HOWTO catch timeout exception in Prolog
I want to limit "executing" of algorithm in prolog. Can you give me a hint, how to do it? I have found this predicate: call_with_time_limit How can I catch the time_limit开发者_如何转开发_exceeded exception? Thanks
UPDATE:
I am trying it this way:
timeout(t) :-
catch(call_with_time_limit(t, sleep(5)), X, error_process(X)).
error_process(time_limit_exceeded) :- write('Timeout exceeded'), nl, halt.
error_process(X) :- write('Unknown Error' : X), nl, halt.
but noting happend when I call timeout(1):
prolog :-
timeout(1),
but when I do it this way:
runStart :- call_with_time_limit(1, sleep(5)).
timeout(1) :-
catch(runStart, X, error_process(X)).
error_process(time_limit_exceeded) :- write('Timeout exceeded'), nl, halt.
error_process(X) :- write('Unknown Error' : X), nl, halt.
and again call timeout(1) everything is fine. Why? Thanks UPDATE 2:
Problem solved, it is necessary to have predcate "argument" with upper case...
Use catch/3
. Example:
catch(call_with_time_limit(1,
sleep(5)),
time_limit_exceeded,
writeln('overslept!')).
More practically:
catch(call_with_time_limit(T, heavy_computation(X)),
time_limit_exceeded,
X = no_answer). % or just fail
loop :- loop.
loop_for_n_sec(N, Catcher) :-
catch(
call_with_time_limit(N, loop),
Catcher,
true
).
Usage:
?- loop_for_n_sec(1, Catcher).
Catcher = time_limit_exceeded
精彩评论