Skip to content

Instantly share code, notes, and snippets.

@camoy
Last active July 13, 2019 21:53
Show Gist options
  • Save camoy/f66ef2ce42ba4cb2eb8c023d4cc89d4a to your computer and use it in GitHub Desktop.
Save camoy/f66ef2ce42ba4cb2eb8c023d4cc89d4a to your computer and use it in GitHub Desktop.
McCarthy's `amb` without mutable state.
(define-values (success-tag fail-tag)
(values (make-continuation-prompt-tag 'success)
(make-continuation-prompt-tag 'fail)))
(define-syntax amb
(syntax-rules ()
[(_ alt ...)
(call/comp
(λ (k)
(abort
(call/prompt
(λ ()
(call/prompt
(λ ()
(let ([x (k alt)])
(abort/cc success-tag (λ () x))))
fail-tag)
...
(if (continuation-prompt-available? fail-tag)
(abort/cc fail-tag void)
(error "amb tree exhausted")))
success-tag)))
(if (continuation-prompt-available? fail-tag)
fail-tag
(default-continuation-prompt-tag)))]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment