Skip to content

Instantly share code, notes, and snippets.

@msullivan
Created August 29, 2012 20:12
Show Gist options
  • Select an option

  • Save msullivan/3518221 to your computer and use it in GitHub Desktop.

Select an option

Save msullivan/3518221 to your computer and use it in GitHub Desktop.
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