3

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...

4

2 回答 2

4

使用catch/3. 例子:

catch(call_with_time_limit(1,
                           sleep(5)),
      time_limit_exceeded,
      writeln('overslept!')).

更实际:

catch(call_with_time_limit(T, heavy_computation(X)),
      time_limit_exceeded,
      X = no_answer).  % or just fail
于 2011-04-19T19:09:42.560 回答
2
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
于 2011-04-19T19:13:00.897 回答