1
1
const INTERNALNAMES = (:__model__ , :__context__ , :__varinfo__ )
2
2
3
3
"""
4
- isassumption(expr)
4
+ isassumption(expr[, vn] )
5
5
6
6
Return an expression that can be evaluated to check if `expr` is an assumption in the
7
7
model.
@@ -13,39 +13,44 @@ Let `expr` be `:(x[1])`. It is an assumption in the following cases:
13
13
but `x[1] === missing`.
14
14
15
15
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 )
19
16
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)))
20
23
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
42
41
else
43
- false
42
+ $ ( maybe_view (expr)) === missing
44
43
end
44
+ else
45
+ false
45
46
end
46
47
end
47
48
end
48
49
50
+ # failsafe: a literal is never an assumption
51
+ isassumption (expr, vn) = :(false )
52
+ isassumption (expr) = :(false )
53
+
49
54
"""
50
55
contextual_isassumption(context, vn)
51
56
@@ -79,9 +84,6 @@ function contextual_isassumption(context::PrefixContext, vn)
79
84
return contextual_isassumption (childcontext (context), prefix (context, vn))
80
85
end
81
86
82
- # failsafe: a literal is never an assumption
83
- isassumption (expr) = :(false )
84
-
85
87
# If we're working with, say, a `Symbol`, then we're not going to `view`.
86
88
maybe_view (x) = x
87
89
maybe_view (x:: Expr ) = :(@views ($ x))
@@ -382,7 +384,7 @@ function generate_tilde(left, right)
382
384
# more selective with our escape. Until that's the case, we remove them all.
383
385
return quote
384
386
$ vn = $ (AbstractPPL. drop_escape (varname (left)))
385
- $ isassumption = $ (DynamicPPL. isassumption (left))
387
+ $ isassumption = $ (DynamicPPL. isassumption (left, vn ))
386
388
if $ isassumption
387
389
$ (generate_tilde_assume (left, right, vn))
388
390
else
@@ -439,7 +441,7 @@ function generate_dot_tilde(left, right)
439
441
@gensym vn isassumption value
440
442
return quote
441
443
$ vn = $ (AbstractPPL. drop_escape (varname (left)))
442
- $ isassumption = $ (DynamicPPL. isassumption (left))
444
+ $ isassumption = $ (DynamicPPL. isassumption (left, vn ))
443
445
if $ isassumption
444
446
$ (generate_dot_tilde_assume (left, right, vn))
445
447
else
0 commit comments