Assignments

Assignments

Maps from the addresses of random choices to their values are stored in associative tree-structured data structures that have the following abstract type:

Gen.AssignmentType.
abstract type Assignment end

Abstract type for maps from hierarchical addresses to values.

source

Assignments are constructed by users to express observations and/or constraints on the traces of generative functions. Assignments are also returned by certain Gen inference methods, and are used internally by various Gen inference methods.

Assignments provide the following methods:

Gen.has_valueFunction.
has_value(assmt::Assignment, addr)

Return true if there is a value at the given address.

source
Gen.get_valueFunction.
value = get_value(assmt::Assignment, addr)

Return the value at the given address in the assignment, or throw a KeyError if no value exists. A syntactic sugar is Base.getindex:

value = assmt[addr]
source
Gen.get_subassmtFunction.
subassmt = get_subassmt(assmt::Assignment, addr)

Return the sub-assignment containing all choices whose address is prefixed by addr.

It is an error if the assignment contains a value at the given address. If there are no choices whose address is prefixed by addr then return an EmptyAssignment.

source
key_subassmt_iterable = get_values_shallow(assmt::Assignment)

Return an iterable collection of tuples (key, value) for each top-level key associated with a value.

source
key_subassmt_iterable = get_subassmts_shallow(assmt::Assignment)

Return an iterable collection of tuples (key, subassmt::Assignment) for each top-level key that has a non-empty sub-assignment.

source
Gen.to_arrayFunction.
arr::Vector{T} = to_array(assmt::Assignment, ::Type{T}) where {T}

Populate an array with values of choices in the given assignment.

It is an error if each of the values cannot be coerced into a value of the given type.

Implementation

To support to_array, a concrete subtype T <: Assignment should implement the following method:

n::Int = _fill_array!(assmt::T, arr::Vector{V}, start_idx::Int) where {V}

Populate arr with values from the given assignment, starting at start_idx, and return the number of elements in arr that were populated.

source
Gen.from_arrayFunction.
assmt::Assignment = from_array(proto_assmt::Assignment, arr::Vector)

Return an assignment with the same address structure as a prototype assignment, but with values read off from the given array.

The order in which addresses are populated is determined by the prototype assignment. It is an error if the number of choices in the prototype assignment is not equal to the length the array.

Implementation

To support from_array, a concrete subtype T <: Assignment should implement the following method:

(n::Int, assmt::T) = _from_array(proto_assmt::T, arr::Vector{V}, start_idx::Int) where {V}

Return an assignment with the same address structure as a prototype assignment, but with values read off from arr, starting at position start_idx, and the number of elements read from arr.

source
Gen.address_setFunction.
addrs::AddressSet = address_set(assmt::Assignment)

Return an AddressSet containing the addresses of values in the given assignment.

source

Note that none of these methods mutate the assignment.

Assignments also provide Base.isempty, which tests of there are no random choices in the assignment, and Base.merge, which takes two assignments, and returns a new assignment containing all random choices in either assignment. It is an error if the assignments both have values at the same address, or if one assignment has a value at an address that is the prefix of the address of a value in the other assignment.

Dynamic Assignment

One concrete assignment type is DynamicAssignment, which is mutable. Users construct DynamicAssignments and populate them for use as observations or constraints, e.g.:

assmt = DynamicAssignment()
assmt[:x] = true
assmt["foo"] = 1.25
assmt[:y => 1 => :z] = -6.3

There is also a constructor for DynamicAssignment that takes initial (address, value) pairs:

assmt = DynamicAssignment((:x, true), ("foo", 1.25), (:y => 1 => :z, -6.3))
struct DynamicAssignment <: Assignment .. end

A mutable map from arbitrary hierarchical addresses to values.

assmt = DynamicAssignment()

Construct an empty map.

assmt = DynamicAssignment(tuples...)

Construct a map containing each of the given (addr, value) tuples.

source
Gen.set_value!Function.
set_value!(assmt::DynamicAssignment, addr, value)

Set the given value for the given address.

Will cause any previous value or sub-assignment at this address to be deleted. It is an error if there is already a value present at some prefix of the given address.

The following syntactic sugar is provided:

assmt[addr] = value
source
Gen.set_subassmt!Function.
set_subassmt!(assmt::DynamicAssignment, addr, subassmt::Assignment)

Replace the sub-assignment rooted at the given address with the given sub-assignment. Set the given value for the given address.

Will cause any previous value or sub-assignment at the given address to be deleted. It is an error if there is already a value present at some prefix of address.

source