WebCHURCH - CheatSheet

last revision 2016/05/19 - feedback is welcome: claus.moebus(at)uni-oldenburg.de -

When working through the WebCHURCH-Tutorial Probabilistic Models of Cognition my impression is that the [official documentation] (https://probmods.org/webchurch/online/ref.html) is only partial compatible with the tutorial. Furthermore, some WebCHURCH-procedures are not compatible with the SCHEME R6RS-Standard (e.g. ‘apply’, ‘case’).

Most expressions are evaluated according applicative-order. When expressions are evaluated according normal-order (= delayed or lazy evaluation) this is mentioned explicitly.

My doc is based on:
1. the WebCHURCH doc
2. the Revised7 Report on the Algorithmic Language Scheme - R7RS
3. Dybvig, The Scheme Programming Language, MIT Press, 2009, 4/e
4. Scott, Programming Language Pragmatics, Morgan Kaufman, 2009, 3/e
5. Lunn et al., The BUGS Book - A Practical Introduction to Bayesian Analysis - , CRC Press, 2013

Numerical Operations

(+ val …)

    • is the name of the built-in operation for addition.
  • val …: A bunch of numeric operands.
  • Returns: sum of the numeric operands.

(- val …)

    • is the name of the built-in operation for subtraction.
  • val …: A bunch of numeric operands.
  • Returns: With two or more arguments, this procedures returns the difference of its arguments, associating to the left.
  • Returns: With one argument, however, it returns the additive inverse of its argument.

(* val …)

    • is the name of the built-in operation for multiplication.
  • val …: A bunch of numeric operands.
  • Returns: product of the numeric operands.

(/ val …)

  • / is the name of the built-in operation for division.
  • val …: A bunch of numeric operands.
  • Returns: With two or more arguments, this procedure returns the quotient of its arguments, associating to the left.
  • Returns: With one argument, however, it returns the multiplicative inverse of its argument.

(< val1 val2 …)

  • Less than ?
  • val1 val2…: A bunch of two ore more numbers.
  • Returns: boolean

(<= val1 val2 …)

  • Less or equal ?
  • val1 val2…: A bunch of two ore more numbers.
  • Returns: boolean

(= val1 val2 …)

  • equal ?
  • val1 val2…: A bunch of two ore more numbers.
  • Returns: boolean

(> val1 val2 …)

  • greater than ?
  • val1 val2…: A bunch of two ore more numbers.
  • Returns: boolean

(>= val1 val2 …)

  • greater or equal ?
  • val1 val2…: A bunch of two ore more numbers.
  • Returns: boolean

A

(and expr …)

  • normal-order evaluation
  • and is a sequential and. If there are no expr s, #t is returned. Otherwise, the expr s are evaluated from left to right until an expr returns #f or the last expr is reached. In the former case, the and expression returns a #f value without evaluating the remaining expressions. In the latter case, the last expression is evaluated and its value should be returned. In fact, WebChurch returns #t in this case.(2013/11/20)
  • expr …: a sequence of expressions with a boolean value
  • Returns: a boolean value

(append lst1 lst2 …)

  • Append some lists together.
  • lsti …: A list.
  • Returns: A concatenated list.

(apply proc obj …)

  • Proc: procedure
  • obj : object or value
  • Returns: the values of applying proc to obj

B

(barplot distribution)

  • distribution: (events probs)
  • events : list of event labels
  • probs : list of corresponding probabilities
  • Returns : a histogram
  • Example : (barplot (list (list 1 2 3) (list .5 0.25 0.25)))
  • Example :
  • (barplot '((0 1) (0.3214285714285714 0.6785714285714285)))

(beta a b)

  • Samples from the Beta distribution B(a,b).
  • a: positive real
  • b: positive real
  • Mean: a/(a + b); Variance: ab/((a+b)^2(a+b+1))
  • Returns: the value sampled from the Beta distribution

(binomial p n)

  • is not implemented (2013/11/23) though it appears in the documentation
  • A random ERP for binomial distributions
  • p: probability of success
  • n: number of independent trials
  • Returns: number

C

(case key case-clause …)

  • key: expression is evaluated and checked whether it is a member of a lst in a case-clause
  • case-clause: (lst expr)
  • lst : list is a list of values and is n o t evaluated
  • Returns: the value of expr if the value of key is equal? to a member of lst
  • Example:
  • (case (* 2 3) ((1 2 3 4 5) "<= 5") ((6 7 8 9 10) ">= 6")) =>  ">= 6"

(cos x)

  • x : argument in radians (e.g.: 90 degrees = pi/2, 45 degrees = pi/4)
  • Example:
  • (cos (/ pi 2)) => 0.0
  • Example:
  • (cos (/ pi 4)) => 0.707106781

D

(define variable expression)

  • normal-order evaluation
  • The definition form describes a definition used to create variable bindings and may appear anywhere other definitions may appear.
  • variable: a symbol which is bound to the value of expression.
  • expression: a constant.
  • expression: the value of a simple or compound procedure (e.g. a lambda-expression).

(define (variable parameter …) body )

  • normal-order evaluation
  • is equivalent to: (define variable (lambda (parameter …) body )).
  • is the definition of a function with name variable and zero or more parameters parameter.

(density list {string})

  • Displays the graph of a probability density function.
  • lst: a list of samples drawn from a continous probability distribution.
  • string: optional string which is used as the title of the density diagram.

(dirichlet lst_of_alphas)

  • Samples a probability distribution from the Dirichlet distribution.
  • lst_of_alphas: list of natural-numbered pseudocounts
  • Returns: A probability distribution (= list of probabilities which sum to 1)

(display {expr … })

  • Displays the value of zero or more expr (including numbers, strings, lists, etc).
  • expr: a Church expression
  • Returns: void

(drop lst n)

  • Drop the first n items from a list. If there are fewer than n items in the list, return the empty list.
  • lst: list
  • n: natural number
  • Returns: remainder of lst starting with the (n+1)th item

E

(enumeration-query defines query-expr condition-expr)

  • normal-order evaluation
  • implements the direct construction of a conditional distribution by evaluating the choice-points of the computation trace, which is efficient and sound but not effective for larger models
  • defines …: Definitions to evaluate – set up bindings for the query
  • query-expr: The expression you want to know about
  • condition-expr = (condition boolean-expr) | boolean-expr.
  • Returns: a conditional distribution; which is a list of two lists. The first list is the set of events, the second list the according probabilities. This list-object could be plotted with barplot.

(equal? obj1 obj2)

  • Check if two things are equivalent. (Recursive on lists, vectors, etc.)
  • obj1 : WebChurch object or value
  • obj2 : WebChurch object or value
  • Returns: boolean (#t or #f)

(eval expr)

  • eval allows programmers to write meta-programs, that construct and evaluate other programs
  • expr : WebChurch expression
  • Returns: the value of expr
  • Example:
  • (define expression '(* 2 3))
  • expression         ; => (* 2 3)
  • (eval expression)  ; => 6
  • (expression)       ; => error 

(exp x)

  • Example:
  • (exp 0) => 1

(expt val1 val2)

  • Raise a number to a power val1^val2
  • val1: A number
  • val2: A number
  • Returns: the value of val1^val2

F

“false” or “#f”

  • boolean constant (must be written without “”)

(fifth lst)

  • lst: the list
  • Returns: the fifth element of the list

(filter pred lst)

  • filters list so that pred is true for all members of the new list
  • pred: unary predicate
  • lst: list which has to be filtered by pred
  • Returns: a filtered list
  • Example:
  • (filter (lambda (x) (> x 3)) (list 1 2 3 4 5)) => (4 5)

(first lst)

  • Get the first element of a list or a pair.
  • lst: The list or pair.
  • Returns: First element of the list or pair.

(flip {weight})

  • Flip a fair or weighted coin (= Bernouilli trial).
  • weight: If given, the coin is biased with this weight.
  • Returns: true or false

(flatten nlst)

  • flattens a nested list (removes the inner parenthesis of nlst)
  • nlst: a nested list
  • Returns: a list of the elements of the nested list

(fold proc init lst)

  • Fold. (You know you want to….)
  • proc: The procedure to apply, which should take two arguments: the next value from lst and the accumulated value.
  • init: The value to start with (initial value for the accumulated value argument of proc).
  • lst: The list to fold over.
  • Returns: the accumulated value
  • Example:
  • (fold *  -1 '(1 2 3 4)) => -24
  • Example:
  • (fold + -24 '(1 2 3 4)) => -14
  • Example:
  • (fold  (lambda (x y) (map + x y)) '(0 0) '((1 2) (2 3) (3 4))) ; ==> (6 9)

(foldl f init lst)

  • Accumulate the result of applying a function to a list left to right
  • f : function to apply
  • init: Seed value for function
  • lst : list to apply the fold over
  • Returns: the accumulated value
  • Example:
  • (foldl + -1 '(1 2 3 4 5)) => 14
  • Example:
  • (foldl (lambda (x y) (map + x y)) '(0 0) '((1 2) (2 3) (3 4))) ; ==> (6 9)

(foldr f init lst)

  • Accumulate the result of applying a function to a list right to left
  • f : function to apply
  • init: Seed value for function
  • lst : list to apply the fold over
  • Returns: the accumulated value
  • Example:
  • (foldr + -1 '(1 2 3 4 5)) => 14
  • Example:
  • (foldr (lambda (x y) (map + x y)) '(0 0) '((1 2) (2 3) (3 4))) ; ==> (6 9)

(fourth lst)

  • Returns the fourth element of a list.
  • lst: the list
  • Returns: the fourth element of the list

G

(gaussian {mu} {sigma})

  • A random ERP for sampling from Gaussian distributions
  • mu: optional mean of the Gaussian (default is 0); can be left out only when sigma is left out
  • sigma: optional standard deviation of the Gaussian (default is 1)
  • Returns: a number sampled from the Normal (Gaussian) Distribution

(geometric weight)

  • is not implemented but defined in chapter 01 of the tutorial
  • The distribution of the number of failures y = x-1 where x is the number of Bernoulli trials (flips of a fair or weighted coin) required for one success to occur.
  • weight: the success probability for each Bernoulli trial
  • Returns: x-1 the number of failures before the success occurs

(get-time)

  • Returns: system time in msec

H

(hist list {string})

  • displays a histogram.
  • lst: a list of samples drawn from a discrete probability distribution
  • string: optional string which is used as the title of the histogram.

I

(if test consequent {alternate})

  • normal-order evaluation
  • test, consequent, and alternate must be expressions.
  • Returns: first, test is evaluated. If it yields a true value, then consequent is evaluated and its values are returned. Otherwise alternate is evaluated and its values are returned.
  • Returns: If test yields #f and no alternate is specified, then the result of the expression is unspecified.

(iota count {start} {step})

  • Create list based on an arithmetic progression
  • count: length of returned list
  • start: first real in list (default is 0)
  • step : real increment (default is 1)
  • Example:
  • (iota 5) => (0 1 2 3 4)
  • Example:
  • (iota 5 -4) => (-4 -3 -2 -1 0)
  • Example:
  • (iota 10 0 1) => (0 1 2 3 4 5 6 7 8 9)
  • Example:
  • (iota 10 0.5 -1.5) => (0.5 -1 -2.5 -4 -5.5 -7 -8.5 -10 -11.5 -13)

J

K

L

(lambda formals body)

  • normal-order evaluation
  • A lambda expression evaluates to a procedure. The environment in effect when the lambda expression is evaluated is remembered as part of the procedure.
  • formals must be a formal parameter list.
  • body consists of one expression (e.g. a lambda expression).

(length lst)

  • Get the length of a list
  • lst: The list.
  • Returns: The length (an integer).

(let ((var expr) …) body)

  • The binding construct let gives Scheme and WebCHURCH a block structure, like Algol 60. In a let expression, the initial values are computed before any of the variables become bound.
  • Semantics: The expr s are evaluated in the current environment (in some unspecified order), the var s are bound to fresh locations holding the results, the body is evaluated in the extended environment, and the values of the last expression of body are returned. Each binding of a var has body as its region. It is an error for a var to appear more than once in the list of variables being bound.
  • body: is a sequence of zero or more definitions followed by a sequence of one or more expressions
  • Returns: the result of evaluating the last expression in body

(let* ((var expr) …) body)

  • Semantics: The let* binding construct is similar to let, but the bindings are performed sequentially from left to right, and the region of a binding indicated by (var expr) is that part of the let* expression to the right of the binding. Thus the second binding is done in an environment in which the first binding is visible, and so on. The var need not be distinct.
  • body: is a sequence of zero or more definitions followed by a sequence of one or more expressions
  • Returns: the result of evaluating the last expression in body

(letrec ((var expr) …) body)

  • Example:
  • (letrec ((even? (lambda (n) (if (= 0 n) #t (odd? (- n 1))))) (odd? (lambda (n) (if (= 0 n) #f (even? (- n 1)))))) (even? 10)) => #t

(lineplot lst)

  • displays a line plot such that successive points are line-connected
  • lst : is a list of points (point …)
  • point: is a 2-element list (x-coordinate y-coordinate)
  • Example:
  • (lineplot '((0 0) (1 -1) (2 1) (3 -2) (4 2) (5 -3) (6 3) (7 -4) (8 4)))

(list obj …)

  • Builds a list of the values of a bunch of obj
  • obj …: Things to make into a list
  • Returns: A list of the values of the obj

(list-elt lst i)

  • Gets the i-th element of a list
  • i : natural number 1 <= i <= length(lst)
  • Returns: i-th element of a list
  • Example:
  • (list-elt '(0 2 4 5) 2) => 2

(log x)

  • Computes the natural logarithm ln(x).
  • Logarithms for other bases b can be obtained by log_b(x) = log(x)/log(b)
  • x : real
  • Returns: the natural log of real x
  • Example:
  • (log 0.5) => -0.6931471805599453

M

(make-list n obj)

  • Make a list of length n where all elements are obj
  • n : is a natural number
  • obj: is the repeated element in the returned list
  • Returns: A list of n objects obj

(map proc list …)

  • Apply a procedure to each element of some lists.
  • proc: The procedure to apply.
  • list …: The lists to map over.
  • Returns: a list of values of procedure applications.

(max list)

  • maximum of numbers
  • list: list of numbers
  • Returns: the maximum
  • Examples:
  • (max -3 -5 -7) => -3
  • (max) => -Infinity
  • (apply max '( 1 3 5 4 2)) => 5

(mem procedure)

  • normal-order evaluation
  • generates a memoized version of procedure.
  • procedure: variable name of a procedure.
  • Returns: a memoized version of procedure.

(mh-query samples lag defines query-expr condition-expr)

  • normal-order evaluation
  • does inference using the Metropolis-Hastings algorithm (as described in Goodman, et al., 2008).
  • samples: The number of samples returned a f t e r filtering.
  • lag: This thinning factor controls filtering; only each lag-th sample will be retained.
  • defines …: A sequence of definitions (usually setting up the generative model).
  • query-expr: The query expression; the value of this expression is o n e sample
  • condition-expr = (condition boolean-expr) | boolean-expr.
  • boolean-expr: Church-expression with a boolean value.
  • Returns: A list of samples from the conditional (posterior) distribution.

(multinomial categs probs )

  • Samples an element from list of category labels categs with the probability specified in list of probs
  • categs list of R category labels (= symbols)
  • probs list of R probabilities (Probability of category membership)
  • Returns: an event (= element of categs)
  • Example:
  • (multinomial (list 'a 'b 'c) (list (/ 1 2) (/ 1 8) (- 1 (/ 1 2) (/ 1 8)))) => a or b or c

N

(null? lst)

  • Check if a list is null or empty.
  • lst: The list.
  • Returns: boolean (#t or #f)
  • Returns #t when the list is equal to ’().

O

(or expr …)

  • normal-order evaluation
  • Or is a sequential or. If there are no expr s, #f is returned. Otherwise, the expr s are evaluated from left to right until a expr returns a #t value or the last expr is reached. In the former case, the or expression returns a #t without evaluating the remaining expressions. In the latter case, the last expression is evaluated and its value is returned. In fact, WebChurch returns the value of expr in all cases.(2013/11/20)
  • expr …: a sequence of expressions with a boolean value.
  • Returns: a boolean value.

P

(pair obj1 obj2)

  • Build a pair (obj1 . obj2)
  • obj1: The first element (can be selected by “first”)
  • obj2: The second element (can be selected by “rest”)
  • Returns: A pair

(pair? obj)

  • Returns #t if obj is a pair (or list)
  • obj: Church object
  • Returns: boolean

Q

(quote datum)

  • normal-order evaluation
  • evaluates to datum
  • datum is any valid Church-expression
  • Returns: datum

datum is equivalent to (quote datum)

  • normal-order evaluation
  • datum is any valid Church-expression.

R

(rejection-query defines query-expr condition-expr)

  • normal-order evaluation
  • implements rejection sampling, which is slow but sound.
  • defines …: Definitions to evaluate – set up bindings for the query
  • query-expr: The expression you want to know about
  • condition-expr = (condition boolean-expr) | boolean-expr.
  • Returns: A conditional sample (a value of query-expr, if the value of condition-expr is true)

(repeat N procedure)

  • Repeats a procedure a given number of times.
  • N: number of times to repeat.
  • procedure: the null-ary procedure (= thunk) to repeat.
  • Returns: a list where each element is the result of a call of proc. If N=0 the list is empty.

(rest lst)

  • Get the tail of a list (all but the first element) or the second element of a pair
  • lst: a list or a pair
  • Returns: tail of list or second element of pair

S

(second lst)

  • Returns the second element of a list.
  • lst: the list
  • Returns: the second element of the list

(seventh lst)

  • Returns the seventh element of a list.
  • lst: the list
  • Returns: the seventh element of the list

(sin x)

  • x : argument in radians (e.g.: 90 degrees = pi/2, 45 degrees = pi/4)
  • Example: (sin (/ pi 2)) => 1.0
  • Example: (sin (/ pi 4)) => 0.707106781

(sixth lst)

  • Returns the sixth element of a list.
  • lst: the list
  • Returns: the sixth element of the list

(sort lst comp)

  • sort a list according to a comparator
  • lst: a list of numbers to be sorted
  • comp: a binary predicate
  • Returns: the sorted list
  • Examples:
  • (sort '(1 3 5 7 6 4 2) > ) => (1 2 3 4 5 6 7) 
  • (sort '(1 3 5 7 6 4 2) < ) => (7 6 5 4 3 2 1) 
  • (sort '(1 3 5 7 6 4 2) = ) => (1 3 5 7 6 4 2) 

(sum lst)

  • Calculates the sum of elements in a list.
  • lst: the list
  • Returns: The sum of the list

T

(take lst n)

  • Get the first n items in a list. If there are fewer than n items in the list, returns just the list.
  • lst: list
  • n: natural number
  • Returns: sublist of lst ending with the n-th item

(third lst)

  • Returns the third element of a list.
  • list: the list
  • Returns: the third element of the list

“true” or “#t”

  • boolean constant (must be written without “”)

U

(uniform-draw lst)

  • Samples a random element from lst according to a uniform pdf
  • lst: a list of objects
  • Returns: object

V

(variance lst)

  • computes the variance
  • lst: a list of sample points
  • Returns: the variance
  • Example:
  • (variance (repeat 100000 (lambda () (gaussian 0 1)))) => 0.9985057629903443

W

X

Y

Z

(zip lst …)

  • zip together lists using longest list as base
  • Returns: a zipped list
  • Example:
  • (zip '(a b c) '(1 2 3 4) '(6 5 4 3 2 1))) => ((a 1 6) (b 2 5) (c 3 4) (null 4 3) (null null 2) (null null 1))