Skip to content

Instantly share code, notes, and snippets.

@bedekelly
Created September 19, 2019 03:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bedekelly/52c4caa0ad744159d07b6ca6a929438a to your computer and use it in GitHub Desktop.
Save bedekelly/52c4caa0ad744159d07b6ca6a929438a to your computer and use it in GitHub Desktop.
List monad (+alternative) for nondeterminism in Python
toss = {"Fair": ["Heads", "Tails"], "Biased": ["Heads", "Heads"]}
@do
def coins():
coin = yield ["Fair", "Biased"]
result = yield toss[coin]
_ = yield guard(result == "Heads")
return coin
# Probability of a biased coin, given you observed Heads, is 2/3.
assert coins() == ["Fair", "Biased", "Biased"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment