Skip to content

Commit 8cb0796

Browse files
committed
Added @prefix macro which calls prefix with a Val argument to
make things easier to basic users
1 parent dc699a5 commit 8cb0796

File tree

2 files changed

+36
-9
lines changed

2 files changed

+36
-9
lines changed

src/contexts.jl

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,34 @@ function prefix(::PrefixContext{Prefix}, vn::VarName{Sym}) where {Prefix,Sym}
294294
end
295295
end
296296

297+
"""
298+
prefix(model::Model, x)
299+
300+
Return `model` but with all random variables prefixed by `x`.
301+
302+
If `x` is known at compile-time, use `Val{x}()` to avoid runtime overheads for prefixing.
303+
"""
304+
prefix(model::Model, x) = contextualize(model, PrefixContext{Symbol(x)}(model.context))
305+
function prefix(model::Model, ::Val{x}) where {x}
306+
contextualize(model, PrefixContext{Symbol(x)}(model.context))
307+
end
308+
309+
"""
310+
@prefix(model, prefix_expr)
311+
312+
Return `model` but with all random variables prefixed by `prefix_expr`.
313+
314+
The result of `prefix_expr` must will be converted to a `Symbol` and used as the prefix.
315+
316+
!!! note
317+
This is effectively just a convenience macro for the method [`prefix(::Model, x)`](@ref),
318+
which automatically converts the result of `prefix_expr` into a `Val` to avoid runtime overheads
319+
for static prefixes. For more control over the prefixing, use the method directly.
320+
"""
321+
macro prefix(model, prefix_expr)
322+
return :($prefix($(esc(model)), $Val{$(esc(prefix_expr))}()))
323+
end
324+
297325
struct ConditionContext{Values,Ctx<:AbstractContext} <: AbstractContext
298326
values::Values
299327
context::Ctx

src/submodel_macro.jl

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -250,16 +250,15 @@ function submodel(prefix_expr, expr, ctx=esc(:__context__))
250250
end
251251

252252
"""
253-
@returned_quantities [prefix=...] model
253+
@returned_quantities model
254254
255255
Run `model` nested inside of another model and return the return-values of the `model`.
256256
257-
Valid expressions for `prefix=...` are:
258-
- `prefix=false`: no prefix is used. This is the default.
259-
- `prefix=expression`: results in the prefix `Symbol(expression)`.
260-
261-
Prefixing makes it possible to run the same model multiple times while keeping track of
262-
all random variables correctly, i.e. without name clashes.
257+
!!! warning
258+
It's generally recommended to use [`prefix(::Model, x)`](@ref) or
259+
[`@prefix(model, prefix_expr)`](@ref) in combination with `@returned_quantities`
260+
to ensure that the variables in `model` are unique and do not clash with other variables in the
261+
parent model or in other submodels.
263262
264263
# Examples
265264
@@ -308,8 +307,8 @@ julia> @model function demo1(x)
308307
end;
309308
310309
julia> @model function demo2(x, y, z)
311-
a = @returned_quantities prefix="sub1" demo1(x)
312-
b = @returned_quantities prefix="sub2" demo1(y)
310+
a = @returned_quantities prefix(demo1(x), Val{:sub1}())
311+
b = @returned_quantities prefix(demo1(y), Val{:sub2}())
313312
return z ~ Uniform(-a, b)
314313
end;
315314
```

0 commit comments

Comments
 (0)