@@ -83,9 +83,6 @@ hasconditioned_nested(::IsLeaf, context, vn) = hasconditioned(context, vn)
8383function hasconditioned_nested (:: IsParent , context, vn)
8484 return hasconditioned (context, vn) || hasconditioned_nested (childcontext (context), vn)
8585end
86- function hasconditioned_nested (context:: PrefixContext , vn)
87- return hasconditioned_nested (collapse_prefix_stack (context), vn)
88- end
8986
9087"""
9188 getconditioned_nested(context, vn)
10198function getconditioned_nested (:: IsLeaf , context, vn)
10299 return error (" context $(context) does not contain value for $vn " )
103100end
104- function getconditioned_nested (context:: PrefixContext , vn)
105- return getconditioned_nested (collapse_prefix_stack (context), vn)
106- end
107101function getconditioned_nested (:: IsParent , context, vn)
108102 return if hasconditioned (context, vn)
109103 getconditioned (context, vn)
@@ -172,9 +166,6 @@ function conditioned(context::ConditionContext)
172166 # precedence over decendants of `context`.
173167 return _merge (context. values, conditioned (childcontext (context)))
174168end
175- function conditioned (context:: PrefixContext )
176- return conditioned (collapse_prefix_stack (context))
177- end
178169
179170struct FixedContext{Values,Ctx<: AbstractContext } <: AbstractContext
180171 values:: Values
@@ -237,9 +228,6 @@ hasfixed_nested(::IsLeaf, context, vn) = hasfixed(context, vn)
237228function hasfixed_nested (:: IsParent , context, vn)
238229 return hasfixed (context, vn) || hasfixed_nested (childcontext (context), vn)
239230end
240- function hasfixed_nested (context:: PrefixContext , vn)
241- return hasfixed_nested (collapse_prefix_stack (context), vn)
242- end
243231
244232"""
245233 getfixed_nested(context, vn)
255243function getfixed_nested (:: IsLeaf , context, vn)
256244 return error (" context $(context) does not contain value for $vn " )
257245end
258- function getfixed_nested (context:: PrefixContext , vn)
259- return getfixed_nested (collapse_prefix_stack (context), vn)
260- end
261246function getfixed_nested (:: IsParent , context, vn)
262247 return if hasfixed (context, vn)
263248 getfixed (context, vn)
@@ -351,117 +336,3 @@ function fixed(context::FixedContext)
351336 # precedence over decendants of `context`.
352337 return _merge (context. values, fixed (childcontext (context)))
353338end
354- function fixed (context:: PrefixContext )
355- return fixed (collapse_prefix_stack (context))
356- end
357-
358- # ##########################################################################
359- # ## Interaction of PrefixContext with ConditionContext and FixedContext ###
360- # ##########################################################################
361-
362- """
363- collapse_prefix_stack(context::AbstractContext)
364-
365- Apply `PrefixContext`s to any conditioned or fixed values inside them, and remove
366- the `PrefixContext`s from the context stack.
367-
368- !!! note
369- If you are reading this docstring, you might probably be interested in a more
370- thorough explanation of how PrefixContext and ConditionContext / FixedContext
371- interact with one another, especially in the context of submodels.
372- The DynamicPPL documentation contains [a separate page on this
373- topic](https://turinglang.org/DynamicPPL.jl/previews/PR892/internals/submodel_condition/)
374- which explains this in much more detail.
375-
376- ```jldoctest
377- julia> using DynamicPPL: collapse_prefix_stack
378-
379- julia> c1 = PrefixContext(@varname(a), ConditionContext((x=1, )));
380-
381- julia> collapse_prefix_stack(c1)
382- ConditionContext(Dict(a.x => 1), DefaultContext())
383-
384- julia> # Here, `x` gets prefixed only with `a`, whereas `y` is prefixed with both.
385- c2 = PrefixContext(@varname(a), ConditionContext((x=1, ), PrefixContext(@varname(b), ConditionContext((y=2,)))));
386-
387- julia> collapsed = collapse_prefix_stack(c2);
388-
389- julia> # `collapsed` really looks something like this:
390- # ConditionContext(Dict{VarName{:a}, Int64}(a.b.y => 2, a.x => 1), DefaultContext())
391- # To avoid fragility arising from the order of the keys in the doctest, we test
392- # this indirectly:
393- collapsed.values[@varname(a.x)], collapsed.values[@varname(a.b.y)]
394- (1, 2)
395- ```
396- """
397- function collapse_prefix_stack (context:: PrefixContext )
398- # Collapse the child context (thus applying any inner prefixes first)
399- collapsed = collapse_prefix_stack (childcontext (context))
400- # Prefix any conditioned variables with the current prefix
401- # Note: prefix_conditioned_variables is O(N) in the depth of the context stack.
402- # So is this function. In the worst case scenario, this is O(N^2) in the
403- # depth of the context stack.
404- return prefix_cond_and_fixed_variables (collapsed, context. vn_prefix)
405- end
406- function collapse_prefix_stack (context:: AbstractContext )
407- return collapse_prefix_stack (NodeTrait (collapse_prefix_stack, context), context)
408- end
409- collapse_prefix_stack (:: IsLeaf , context) = context
410- function collapse_prefix_stack (:: IsParent , context)
411- new_child_context = collapse_prefix_stack (childcontext (context))
412- return setchildcontext (context, new_child_context)
413- end
414-
415- """
416- prefix_cond_and_fixed_variables(context::AbstractContext, prefix::VarName)
417-
418- Prefix all the conditioned and fixed variables in a given context with a single
419- `prefix`.
420-
421- ```jldoctest
422- julia> using DynamicPPL: prefix_cond_and_fixed_variables, ConditionContext
423-
424- julia> c1 = ConditionContext((a=1, ))
425- ConditionContext((a = 1,), DefaultContext())
426-
427- julia> prefix_cond_and_fixed_variables(c1, @varname(y))
428- ConditionContext(Dict(y.a => 1), DefaultContext())
429- ```
430- """
431- function prefix_cond_and_fixed_variables (ctx:: ConditionContext , prefix:: VarName )
432- # Replace the prefix of the conditioned variables
433- vn_dict = to_varname_dict (ctx. values)
434- prefixed_vn_dict = Dict (
435- AbstractPPL. prefix (vn, prefix) => value for (vn, value) in vn_dict
436- )
437- # Prefix the child context as well
438- prefixed_child_ctx = prefix_cond_and_fixed_variables (childcontext (ctx), prefix)
439- return ConditionContext (prefixed_vn_dict, prefixed_child_ctx)
440- end
441- function prefix_cond_and_fixed_variables (ctx:: FixedContext , prefix:: VarName )
442- # Replace the prefix of the conditioned variables
443- vn_dict = to_varname_dict (ctx. values)
444- prefixed_vn_dict = Dict (
445- AbstractPPL. prefix (vn, prefix) => value for (vn, value) in vn_dict
446- )
447- # Prefix the child context as well
448- prefixed_child_ctx = prefix_cond_and_fixed_variables (childcontext (ctx), prefix)
449- return FixedContext (prefixed_vn_dict, prefixed_child_ctx)
450- end
451- function prefix_cond_and_fixed_variables (c:: AbstractContext , prefix:: VarName )
452- return prefix_cond_and_fixed_variables (
453- NodeTrait (prefix_cond_and_fixed_variables, c), c, prefix
454- )
455- end
456- function prefix_cond_and_fixed_variables (
457- :: IsLeaf , context:: AbstractContext , prefix:: VarName
458- )
459- return context
460- end
461- function prefix_cond_and_fixed_variables (
462- :: IsParent , context:: AbstractContext , prefix:: VarName
463- )
464- return setchildcontext (
465- context, prefix_cond_and_fixed_variables (childcontext (context), prefix)
466- )
467- end
0 commit comments