Created
August 29, 2012 20:12
-
-
Save msullivan/3518221 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| val state : unit cont ref = | |
| ref (callcc (fn escape : unit cont cont => | |
| (callcc (fn k => throw escape k); | |
| raise AmbFail))) | |
| fun amb [] = throw (!state) () | |
| | amb (x::xs) = | |
| let val old = !state | |
| in callcc (fn escape => | |
| (callcc (fn k => (state := k; throw escape x)); | |
| state := old; | |
| amb xs)) | |
| end | |
| (* Collect all possible results of the nondeterministic | |
| * computation t (that is, t might use amb) *) | |
| fun collect t = | |
| let val old = !state | |
| val results = ref [] | |
| val () = callcc | |
| (fn finished => | |
| (state := finished; | |
| results := t () :: !results; | |
| amb [] (* backtrack *))) | |
| val () = state := old | |
| in rev (!results) end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment