probcomp / Gen.jl

A general-purpose probabilistic programming system with programmable inference
https://gen.dev
Apache License 2.0
1.79k stars 160 forks source link

`Gen.choicemap` constructor assigns choice-map args as leaves #517

Open sritchie opened 11 months ago

sritchie commented 11 months ago

In this version of the constructor: https://github.com/probcomp/Gen.jl/blob/master/src/choice_map.jl#L651-L661

This line:

choices[addr] = value

assigns the value as a leaf always, leading to this erro:

julia> cm = Gen.choicemap(:k => Gen.choicemap(:v => 2))
│
└── :k : DynamicChoiceMap(Dict{Any, Any}(:v => 2), Dict{Any, Any}())

julia> Gen.has_value(cm, :k)
true

julia> Gen.has_submap(cm, :k)
false
fsaad commented 11 months ago

This behavior is the desired one, per the documentation of Gen.set_value! and Gen.set_submap!.

For the example above, the API requires breaking down the construction into separate calls:

julia> cm = Gen.choicemap()
julia> Gen.set_submap!(cm, :k, Gen.choicemap(:v =>2))
sritchie commented 11 months ago

I would recommend that the constructor check and guard against this case though, yeah?