@@ -502,7 +502,7 @@ function conditioned(context::ConditionContext)
502
502
# precedence over decendants of `context`.
503
503
return _merge (context. values, conditioned (childcontext (context)))
504
504
end
505
- function conditioned (context:: PrefixContext{Prefix} ) where {Prefix}
505
+ function conditioned (context:: PrefixContext )
506
506
return conditioned (collapse_prefix_stack (context))
507
507
end
508
508
@@ -681,6 +681,9 @@ function fixed(context::FixedContext)
681
681
# precedence over decendants of `context`.
682
682
return _merge (context. values, fixed (childcontext (context)))
683
683
end
684
+ function fixed (context:: PrefixContext )
685
+ return fixed (collapse_prefix_stack (context))
686
+ end
684
687
685
688
"""
686
689
collapse_prefix_stack(context::AbstractContext)
@@ -691,7 +694,22 @@ the `PrefixContext`s from the context stack.
691
694
```jldoctest
692
695
julia> using DynamicPPL: collapse_prefix_stack
693
696
694
- julia> c1 = PrefixContext({:a}(ConditionContext((x=1, )))
697
+ julia> c1 = PrefixContext{:a}(ConditionContext((x=1, )));
698
+
699
+ julia> collapse_prefix_stack(c1)
700
+ ConditionContext(Dict(a.x => 1), DefaultContext())
701
+
702
+ julia> # Here, `x` gets prefixed only with `a`, whereas `y` is prefixed with both.
703
+ c2 = PrefixContext{:a}(ConditionContext((x=1, ), PrefixContext{:b}(ConditionContext((y=2,)))));
704
+
705
+ julia> collapsed = collapse_prefix_stack(c2);
706
+
707
+ julia> # `collapsed` really looks something like this:
708
+ # ConditionContext(Dict{VarName{:a}, Int64}(a.b.y => 2, a.x => 1), DefaultContext())
709
+ # To avoid fragility arising from the order of the keys in the doctest, we test
710
+ # this indirectly:
711
+ collapsed.values[@varname(a.x)], collapsed.values[@varname(a.b.y)]
712
+ (1, 2)
695
713
```
696
714
"""
697
715
function collapse_prefix_stack (context:: PrefixContext{Prefix} ) where {Prefix}
@@ -703,7 +721,14 @@ function collapse_prefix_stack(context::PrefixContext{Prefix}) where {Prefix}
703
721
# depth of the context stack.
704
722
return prefix_cond_and_fixed_variables (collapsed, VarName {Prefix} ())
705
723
end
706
- collapse_prefix_stack (context:: AbstractContext ) = context
724
+ function collapse_prefix_stack (context:: AbstractContext )
725
+ return collapse_prefix_stack (NodeTrait (collapse_prefix_stack, context), context)
726
+ end
727
+ collapse_prefix_stack (:: IsLeaf , context) = context
728
+ function collapse_prefix_stack (:: IsParent , context)
729
+ new_child_context = collapse_prefix_stack (childcontext (context))
730
+ return setchildcontext (context, new_child_context)
731
+ end
707
732
708
733
"""
709
734
prefix_cond_and_fixed_variables(context::AbstractContext, prefix::VarName)
0 commit comments