Last active
November 30, 2023 18:51
-
-
Save patham9/077a1b8772396251e46b63bd2a60ebce to your computer and use it in GitHub Desktop.
Parallel amb (Chicken Scheme)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(define-syntax amb-parallel | |
(syntax-rules () | |
((_ arg1 arg2) | |
(receive (pipefd0 pipefd1) (create-pipe) | |
(let ((pid (process-fork))) | |
(if (eq? pid 0) | |
(let ((pipefd1_port (open-output-file* pipefd1))) | |
(write (amb-collect arg2) pipefd1_port) ;we can't backtrack into parent so we collect all of this level's results | |
(close-output-port pipefd1_port) | |
(exit 0)) ;child is done | |
(let* ((res1 (amb-collect arg1)) ;we cannot backtrack here either, the child counts on the parent, so we collect all of this level's results | |
(res2 (read (open-input-file* pipefd0)))) ;after we collected our results we can collect the result of the child | |
(if (and (null? res1) (null? res2)) ;but from here we can backtrack safely yet again | |
((amb-failure-continuation)) | |
(if (null? res1) | |
(amb1 res2) | |
(if (null? res2) | |
(amb1 res1) | |
(amb1 (list (amb1 res1) (amb1 res2))))))))))) | |
((_ arg1 arg2 argi ...) | |
(amb1 (amb-parallel (amb-collect arg1) (amb-collect (amb-parallel arg2 argi ...))))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment