### 3Primitive Stochastic Functions

This language consists of four primitive stochastic functions:
• sample draw a sample from a distribution

• mem apply memoization to a (stochastic) function

• fail represents a predicate observation failure; abort the current program execution

• observe-sample represents a point observation; weights the current program evaluation by the likelihood of drawing the given value from the given distribution

The implementation of these features depends on the sampler/solver context in which they are executed. In a mh-sampler context, for example, sample might reuse a choice from a previous run, subject to random perturbations. In an enumerate context, sample will fork its context and potentially the consequences of all values in its distribution.

 procedure(sample dist) → any dist : dist?
Returns a sample from dist. The sample procedure cooperates with the enclosing sampler/solver, unlike dist-sample.

See also Sampling Convenience Functions.

 procedure(mem f) → procedure? f : procedure?
Returns a memoized version of f.

In general, a memoized function must not be called outside of the dynamic extent of the sampler/solver context in which it was created.

Examples:

> (define f (mem (lambda (n) (bernoulli))))
> (f 1)

0

> (f 1) ; calling (f 1) again will get the same value

0

> (for/list ([i 10]) (f i))

'(0 0 1 0 0 0 1 1 0 0)

> (for/list ([i 10]) (f i))

'(0 0 1 0 0 0 1 1 0 0)

 procedure(fail [reason]) → any reason : any/c = #f
Used to express observation failure. When used within a sampler or solver, it typically causes the sampler/solver to try again with different values for the previous choices.

For example, the following function models two coin flips where at least one of them is known to be heads (#t):

 (define (flip-two-coins/one-heads) (define A (flip)) (define B (flip)) (unless (or A B) (fail)) (list A B))

 procedure(observe-sample dist value) → void? dist : dist? value : any/c
Represents observing the value of a random variable distributed as dist being value. Typically, the effect is to adjust the likelihood of the current sampler/solver execution.