3

Can anyone explain why the following YAP code does not result in the variable Result being unified with time_out?

?- time_out(sleep(3),1,Result).
Result = success.
?- time_out(sleep(3),2,Result).
Result = success.
 ?- time_out(sleep(3),1000,Result).
Result = success.
 ?- time_out(sleep(3),2000,Result).

According to the documentation, the predicate should work as follows:

time_out(+Goal, +Timeout, -Result)

Execute goal Goal with time limited Timeout, where Timeout is measured in milliseconds. If the goal succeeds, unify Result with success. If the timer expires before the goal terminates, unify Result with time_out.

repeat
  • 18,496
  • 4
  • 54
  • 166
S0rin
  • 1,283
  • 1
  • 10
  • 22

2 Answers2

3

YAP's documentation also states that:

Last, even though the timer is set in milliseconds, the current implementation relies on alarm/3, and therefore can only offer precision on the scale of seconds.

In turn, the documentation for sleep/1 states:

Block the current thread for Time seconds.

But the problem in this case is likely not related to the times but for the call to sleep/1 blocking the current thread where the time_out/3 call is being itself executed. Nevertheless, I tried the time_out/3 predicate with other goals and also couldn't not get the expected time out. Thus, there might be some bug in it.

Paulo Moura
  • 18,373
  • 3
  • 23
  • 33
  • 1
    I made the YAP developer aware of this thread. I suggest you contact him directly or make a post to the YAP mailing list. – Paulo Moura Oct 12 '15 at 14:59
3

(Somehow I missed this thread)

First, sleep/1 and time_out/3 do not flock together. time_out/3 waits for a certain amount of milliseconds of CPU-time or thread-time, whereas sleep/1 sleeps real-time seconds. There are certain limitations in YAP compared to the original in SICStus, see the manual for more.

In the current development version from the git (last commit d5ce9a137668fe1ae34e2d47c91fc2725ae04a5f, Date: Wed Apr 22 14:21:20 2015 -0600), the mechanism seems to be broken.

In an older version of 6.3.4 I get:

YAP 6.3.4 (x86_64-linux): Tue Jan 29 12:39:29 CET 2013
MYDDAS version MYDDAS-0.9.1
 ?- use_module(library(timeout)).
 % reconsulting /opt/gupu/share/Yap/timeout.yap...
  % reconsulting /opt/gupu/share/Yap/hacks.yap...
  % reconsulted /opt/gupu/share/Yap/hacks.yap in module yap_hacks, 0 msec 15472 bytes
 % reconsulted /opt/gupu/share/Yap/timeout.yap in module timeout, 0 msec 48240 bytes
true.
 ?- 100000000=J,time(time_out((between(1,J,N),N=J),1,R)).
% 0.008 CPU in 0.007 seconds (114% CPU)
J = 100000000,
R = time_out.
 ?- 100000000=J,time(time_out((between(1,J,N),N=J),10000,R)).
% 9.985 CPU in 10.003 seconds ( 99% CPU)
J = 100000000,
R = time_out.
 ?- 100000000=J,time(time_out((between(1,J,N),N=J),100000,R)).
% 23.477 CPU in 23.522 seconds ( 99% CPU)
J = N = 100000000,
R = success.
 ?- 100000000=J,time(time_out(sleep(10),1,R)).
% 0.000 CPU in 10.000 seconds (Inf% CPU)
J = 100000000,
R = success.
Paulo Moura
  • 18,373
  • 3
  • 23
  • 33
false
  • 10,264
  • 13
  • 101
  • 209