Skip to content

Commit 28c6004

Browse files
committed
Re-opening of #310 (#330)
We forgot to merge the #310 before merging #295.
1 parent 0bb2504 commit 28c6004

File tree

1 file changed

+33
-31
lines changed

1 file changed

+33
-31
lines changed

src/compiler.jl

Lines changed: 33 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const INTERNALNAMES = (:__model__, :__context__, :__varinfo__)
22

33
"""
4-
isassumption(expr)
4+
isassumption(expr[, vn])
55
66
Return an expression that can be evaluated to check if `expr` is an assumption in the
77
model.
@@ -13,39 +13,44 @@ Let `expr` be `:(x[1])`. It is an assumption in the following cases:
1313
but `x[1] === missing`.
1414
1515
When `expr` is not an expression or symbol (i.e., a literal), this expands to `false`.
16-
"""
17-
function isassumption(expr::Union{Symbol,Expr})
18-
vn = gensym(:vn)
1916
17+
If `vn` is specified, it will be assumed to refer to a expression which
18+
evaluates to a `VarName`, and this will be used in the subsequent checks.
19+
If `vn` is not specified, `AbstractPPL.drop_escape(varname(expr))` will be
20+
used in its place.
21+
"""
22+
function isassumption(expr::Union{Expr,Symbol}, vn=AbstractPPL.drop_escape(varname(expr)))
2023
return quote
21-
let $vn = $(AbstractPPL.drop_escape(varname(expr)))
22-
if $(DynamicPPL.contextual_isassumption)(__context__, $vn)
23-
# Considered an assumption by `__context__` which means either:
24-
# 1. We hit the default implementation, e.g. using `DefaultContext`,
25-
# which in turn means that we haven't considered if it's one of
26-
# the model arguments, hence we need to check this.
27-
# 2. We are working with a `ConditionContext` _and_ it's NOT in the model arguments,
28-
# i.e. we're trying to condition one of the latent variables.
29-
# In this case, the below will return `true` since the first branch
30-
# will be hit.
31-
# 3. We are working with a `ConditionContext` _and_ it's in the model arguments,
32-
# i.e. we're trying to override the value. This is currently NOT supported.
33-
# TODO: Support by adding context to model, and use `model.args`
34-
# as the default conditioning. Then we no longer need to check `inargnames`
35-
# since it will all be handled by `contextual_isassumption`.
36-
if !($(DynamicPPL.inargnames)($vn, __model__)) ||
37-
$(DynamicPPL.inmissings)($vn, __model__)
38-
true
39-
else
40-
$(maybe_view(expr)) === missing
41-
end
24+
if $(DynamicPPL.contextual_isassumption)(__context__, $vn)
25+
# Considered an assumption by `__context__` which means either:
26+
# 1. We hit the default implementation, e.g. using `DefaultContext`,
27+
# which in turn means that we haven't considered if it's one of
28+
# the model arguments, hence we need to check this.
29+
# 2. We are working with a `ConditionContext` _and_ it's NOT in the model arguments,
30+
# i.e. we're trying to condition one of the latent variables.
31+
# In this case, the below will return `true` since the first branch
32+
# will be hit.
33+
# 3. We are working with a `ConditionContext` _and_ it's in the model arguments,
34+
# i.e. we're trying to override the value. This is currently NOT supported.
35+
# TODO: Support by adding context to model, and use `model.args`
36+
# as the default conditioning. Then we no longer need to check `inargnames`
37+
# since it will all be handled by `contextual_isassumption`.
38+
if !($(DynamicPPL.inargnames)($vn, __model__)) ||
39+
$(DynamicPPL.inmissings)($vn, __model__)
40+
true
4241
else
43-
false
42+
$(maybe_view(expr)) === missing
4443
end
44+
else
45+
false
4546
end
4647
end
4748
end
4849

50+
# failsafe: a literal is never an assumption
51+
isassumption(expr, vn) = :(false)
52+
isassumption(expr) = :(false)
53+
4954
"""
5055
contextual_isassumption(context, vn)
5156
@@ -79,9 +84,6 @@ function contextual_isassumption(context::PrefixContext, vn)
7984
return contextual_isassumption(childcontext(context), prefix(context, vn))
8085
end
8186

82-
# failsafe: a literal is never an assumption
83-
isassumption(expr) = :(false)
84-
8587
# If we're working with, say, a `Symbol`, then we're not going to `view`.
8688
maybe_view(x) = x
8789
maybe_view(x::Expr) = :(@views($x))
@@ -382,7 +384,7 @@ function generate_tilde(left, right)
382384
# more selective with our escape. Until that's the case, we remove them all.
383385
return quote
384386
$vn = $(AbstractPPL.drop_escape(varname(left)))
385-
$isassumption = $(DynamicPPL.isassumption(left))
387+
$isassumption = $(DynamicPPL.isassumption(left, vn))
386388
if $isassumption
387389
$(generate_tilde_assume(left, right, vn))
388390
else
@@ -439,7 +441,7 @@ function generate_dot_tilde(left, right)
439441
@gensym vn isassumption value
440442
return quote
441443
$vn = $(AbstractPPL.drop_escape(varname(left)))
442-
$isassumption = $(DynamicPPL.isassumption(left))
444+
$isassumption = $(DynamicPPL.isassumption(left, vn))
443445
if $isassumption
444446
$(generate_dot_tilde_assume(left, right, vn))
445447
else

0 commit comments

Comments
 (0)