amb:   Ambiguous Operator
amb
for/  amb
for*/  amb
insert-amb-node*!
current-amb-shuffler
current-amb-queue
current-amb-dequeue!
current-amb-enqueue!
8.12

amb: Ambiguous Operator🔗ℹ

 (require amb) package: amb
 (require typed/amb)

syntax

(amb expr ...)

The amb operator.

syntax

(for/amb (for-clause ...) body-or-break ... body)

syntax

(for*/amb (for-clause ...) body-or-break ... body)

Iterate like for/list and for*/list respectively, they allow programmers to explore different possibilities in a non-deterministic way.

Examples:
> (parameterize ([current-amb-queue (make-queue)])
    (let ([x (for/amb ([i (in-range 10)])
               (displayln i)
               i)])
      (when (< x 5) (amb))))

0

1

2

3

4

5

> (with-handlers ([exn:fail:contract? void])
    (parameterize ([current-amb-queue (make-queue)])
      (let ([x (for/amb ([i (in-range 10)]) i)])
        (when (even? x) (amb))
        (displayln x)
        (amb))))

1

3

5

7

9

> (parameterize ([current-amb-queue (make-queue)])
    (let-values ([(x y)
                  (for/amb ([v (in-list '([2 9] [9 2]))])
                    (apply values v))])
      (when (> x y) (amb))
      (displayln (list x y))))

(2 9)

procedure

(insert-amb-node*! k alt*)  void?

  k : (-> any/c ... none/c)
  alt* : (listof (-> any))
Inserts new amb nodes for all alternatives in alt* into the current amb queue. An amb node is a thunk that calls k with the values produced by an alternative.

parameter

(current-amb-shuffler)  (-> list? list?)

(current-amb-shuffler amb-shuffler)  void?
  amb-shuffler : (-> list? list?)
A parameter that determines how to shuffle the alternatives before inserting them into the amb queue. The default value is reverse.

parameter

(current-amb-queue)  queue?

(current-amb-queue amb-queue)  void?
  amb-queue : queue?
A parameter that holds the queue of amb nodes to be evaluated. The queue is initially empty and is populated by insert-amb-node*!.

parameter

(current-amb-dequeue!)  (-> queue? (-> none/c))

(current-amb-dequeue! amb-dequeue!)  void?
  amb-dequeue! : (-> queue? (-> none/c))
A parameter that determines how to dequeue an amb node from the queue. The default value is dequeue!, which means the node at the front of the queue is removed and returned.

parameter

(current-amb-enqueue!)  (-> queue? (-> none/c) void?)

(current-amb-enqueue! amb-enqueue!)  void?
  amb-enqueue! : (-> queue? (-> none/c) void?)
A parameter that determines how to enqueue an amb node into the queue. The default value is enqueue-front!, which means the node is added to the front of the queue.