Skip to content

Instantly share code, notes, and snippets.

@vedantk
Created April 3, 2011 06:00
Show Gist options
  • Save vedantk/900233 to your computer and use it in GitHub Desktop.
Save vedantk/900233 to your computer and use it in GitHub Desktop.
Create transitive predicates in Python.
def transitivePredicate(db):
def predicate(a, b):
pool = db.get(a)
if not pool:
return False
if b in pool:
return True
for ent in pool:
if predicate(ent, b):
return True
return False
return predicate
isTaller = transitivePredicate({
"me": ["you"],
"you": ["him"],
"him": ["that", "it"],
"it": ["that"],
"that": ["this"],
})
assert isTaller("me", "you")
assert isTaller("you", "him")
assert isTaller("me", "him")
assert isTaller("you", "this")
assert not isTaller("you", "me")
assert not isTaller("that", "you")
assert isTaller("it", "this")
assert isTaller("you", "it")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment