Quick Reference

From Church Wiki
Jump to: navigation, search

Church syntax is largely identical to scheme syntax, and most Church primitives do the same thing as their scheme counterparts.

This page lists available primitives, noting special considerations.


Special functions and sugar

lambda, if, mem, get-current-environment, quote (which can be written with single quote mark, ')

eval, apply

define (Allows recursive definitions, but only use it at the top level!!)

begin (Evaluates all subexpressions in order, but only returns value of last. Since Church has no mutation, this is useful only for displaying things.)

let, let*, named-let, case, cond

mem-rec (This is sugar for creating a recursive memoized function. Use as: (mem-rec A (lambda args body)), where A is to be the name of the function and can be used in body to make recursive calls.)

marg (Build a random primitive by attaching a marginal probability function and optional proposer.)

Numbers, strings, and the empty list (written '() or ()) are also available. (They are self-evaluating.)

Standard environment

true, false

first, rest, pair (These replace the antiquated scheme commands car, cdr, cons.)

list, list-ref, list-elt (a 1-indexed version of list-ref), length, append, null?, list?, iota, make-list

list->vector, vector->list, vector-ref

eq?, equal?,


canonicalize-symbol-list (turn a list of symbols into a list of integers -- useful for returning lists of gensyms)

and, or, xor, not (These operators take a variable number of arguments, as in Scheme, but they don't short-circuit as Scheme does. Thus, don't use them for flow control.)

number?, +, -, =, *, /, <, >, <=, >=, exp, log, expt, modulo, min, max


log-flip (A version of flip that takes log-probability parameter, to avoid underflow.)

sample-integer (Sample a random integer between 0 and arg-1.)

sample-discrete (Sample an integer according to a discrete distribution.)


beta, gamma, exponential

uniform (Uniform real number within a range.)

no-proposals (this is the identity function, but tells the inference engine not to make proposals to any erp inside it. use this carefully -- it can save work when there is an erp that is known to be unchangeable, such as (equal? (erp) data), but can make the sampler wrong if used on something that should be sampled.)

In addition to these, any deterministic non-higher-order function in scheme and any distribution with known sampler and marginal can be bound in as a primitive (see standard-env.ss for examples).

Standard preamble

Y (a fixed-point combinator)

uniform-draw (Draw uniformly from a list.)


second, ..., seventh

all, any, product, sum

repeat, fold, reverse, map, multi-map, filter

rejection-query, rejection-lex-query

DPmem, PYmem

dirichlet, make-multinomial-dirichlet, make-symmetric-multinomial-dirichlet (these make all draws at once -- sequential versions coming soon)

Query operators


mh-lex-query: This is the simplest MCMC implementation of lex-query. Calls have the form:

(mh-lex-query N 
  <lex-block> <query-expr> <cond-expr> (get-current-environment))

Where N is the number of MCMC iterations to run before returning a sample, <lex-block> is a set of bindings (form identical to a let statement) that will be available in <query-expr> and <cond-expr>. The <query-expr> and <cond-expr>are (quoted) expressions. For example:

(mh-lex-query N '((a (flip))(b (flip))) 'a '(and a b) (get-current-environment))


(repeated-mh-lex-query N M 
  <lex-block> <query-expr> <cond-expr> (get-current-environment))

Runs an MCMC chain, returns M samples with lag of N iterations between samples.


(tempered-mh-lex-query <vars> <ladders> <iter> <swap-rate> <swaps>
  <lex-block> <query-expr> <cond-expr> (get-current-environment))

<vars> is a list of variables to temper (which become available to be used in the query), <ladders> is a list of lists of parameter values to use for each variable.

(tempered-mh-lex-query '(noise) '((0.01 0.1 0.5)) 10 5 5
  '((a (flip))
    (b (flip)))
  '(if (flip noise) true (and a b))

If the first variable is "posterior-temperature", then the posterior temperature of the whole chain will be tempered.

These query procedures shouldn't be nested in each other or themselves! However, rejection query can be nested.

Personal tools