Skip to content
selfsame edited this page Jan 18, 2015 · 2 revisions

A predicate function takes one argument and returns a boolean.

Dispatch rules require predicate functions for each argument.

def number(x): return isinstance(x, (int, float))
def zero(x): return x == 0

@check(zero, number)
def divide(a, b):
  report("The fabric of the universe grinds to a halt!")
  return False

@given(number, number)
def divide(a, b):
  return a / b

For general purpose use, predicates can serve as a type signature. mud.core includes the standard type predicates:

anything number integer string function 
sequential dictionary module undefined empty

composition

Predicates are easy to compose into new predicates. The following composition functions are provided:

a(even, number)
a(non(zero), even, number)

binding

To avoid import dependencies, predicates can be bound to strings.

bind.predicate("zero", zero)

#predicate guards can take bound strings
@given("zero")
def square(n): return 0

#composition functions can take bound strings
a(non("zero"), "number")

string representations are looked up on invokation. A game/ module that rebinds a predicate will replace it for every bound usage in the application.

predicates and components

predicate composition works well with the entity component compositions. (the provided has function is a generator for predicate key existence functions)

bind.predicate("thing", dictionary)
bind.predicate("positioned", a(has("x"), has("y"))
bind.predicate("dimensional", a(has("width"), has("height"))
bind.predicate("rectangle", a("dimensional", "positioned", "thing"))

@given("rectangle")
def left(e):
  return e["x"] - e["width"]/2
Clone this wiki locally