2

I would like to use racket lisp engines which allow execution of a procedure that can be interrupted by a timer timeout. I'm not sure how to construct a procedure that engine will accept because I can't find examples online. At the engine documentation it lists input proc having the following contract:

(engine proc) → engine?
  proc : ((any/c . -> . void?) . -> . any/c)

I'm just learning typed racket annotation semantics and this is above my head at the moment. Can someone help provide some concrete examples of valid procedures that can be use in a racket engine?

durandaltheta
  • 217
  • 2
  • 8

1 Answers1

3

I've played a little around with it. Here is what I did:

#lang racket
(require racket/engine)

(define (test-engine allow-interrupt)
  (let loop ((n 1))
    (allow-interrupt #f)
    (displayln (list 'step n))
    (allow-interrupt #t)
    (sleep 1)
    (loop (add1 n))))

(define tee (engine test-engine))
(engine-run 2000 tee)

I noticed that it might break in the middle of a displayln so to make displayln atomic I made use of the supplied procedure that makes delays the interruption during atomic operations. Without it it will print out the rest in the next (engine-run 2000 tee) instead of finishing it before it returns.

Sylwester
  • 47,942
  • 4
  • 47
  • 79
  • This is basically what I'm looking for! Question, how are you treating the provided boolean as a procedure? It looks like you're invoking the variable as a set! function – durandaltheta Oct 30 '18 at 17:28
  • @durandaltheta According to the documentation "The argument to proc is a procedure that takes a boolean". It could never be a boolean itself since changing it would change a local binding and not affect anything outside. With a procedure it can alter its environment, which is hidden from us. It's basically message passing so it can be seen as OO. – Sylwester Oct 30 '18 at 17:32
  • Aha, I misread that documentation, which was the core source of my confusion. Now it all makes sense, thanks! – durandaltheta Oct 30 '18 at 17:33