A selection represents a set of addresses of random choices. Selections allow users to specify to which subset of the random choices in a trace a given inference operation should apply.

An address that is added to a selection indicates that either the random choice at that address should be included in the selection, or that all random choices made by a generative function traced at that address should be included. For example, consider the following selection:

selection = select(:x, :y)

If we use this selection in the context of a trace of the function baz below, we are selecting two random choices, at addresses :x and :y:

@gen function baz()
    @trace(bernoulli(0.5), :x)
    @trace(bernoulli(0.5), :y)

If we use this selection in the context of a trace of the function bar below, we are actually selecting three random choices–-the one random choice made by bar at address :x and the two random choices made by foo at addresses :y => :z and :y => :w`:

@gen function foo()
    @trace(normal(0, 1), :z)
    @trace(normal(0, 1), :w)

@gen function bar()
    @trace(bernoulli(0.5), :x)
    @trace(foo(), :y)

There is an abstract type for selections:

abstract type Selection end

Abstract type for selections of addresses.

All selections implement the following methods:, selection)

Is the address selected?

Base.getindex(selection, addr)

Get the subselection at the given address.


Is the selection guaranteed to be empty?


Return a shallow, compile-time address schema, where T is the concrete type of the selection.


There are various concrete types for selections, each of which is a subtype of Selection. Users can construct selections with the select and selectall methods:

selection = select(addrs...)

Return a selection containing a given set of addresses.


selection = select(:x, "foo", :y => 1 => :z)
selection = select()
selection = select(:x => 1, :x => 2)
selection = selectall()

Construct a selection that includes all random choices.


The select method returns a selection with concrete type DynamicSelection. The selectall method returns a selection with concrete type AllSelection. The full list of concrete types of selections is shown below. Most users need not worry about these types. Note that only selections of type DynamicSelection are mutable (using push! and set_subselection!).

struct EmptySelection <: Selection end

A singleton type for a selection that is always empty.

struct AllSelection <: Selection end

A singleton type for a selection that contains all choices at or under an address.

abstract type HierarchicalSelection <: Selection end

Abstract type for selections that have a notion of sub-selections.


Return an iterator over pairs of addresses and subselections at associated addresses.

struct DynamicSelection <: HierarchicalSelection .. end

A hierarchical, mutable, selection with arbitrary addresses.

Can be mutated with the following methods:

Base.push!(selection::DynamicSelection, addr)

Add the address and all of its sub-addresses to the selection.


selection = select()
@assert !(:x in selection)
push!(selection, :x)
@assert :x in selection
set_subselection!(selection::DynamicSelection, addr, other::Selection)

Change the selection status of the given address and its sub-addresses that defined by other.


selection = select(:x)
@assert :x in selection
subselection = select(:y)
set_subselection!(selection, :x, subselection)
@assert (:x => :y) in selection
@assert !(:x in selection)

Note that set_subselection! does not copy data in other, so other may be mutated by a later calls to set_subselection! for addresses under addr.

struct StaticSelection{T,U} <: HierarchicalSelection .. end

A hierarchical selection whose keys are among its type parameters.