Skip to content

Commit 26ef662

Browse files
authored
Permit dot call (like Distributions.Normal) to be used in model definition (#237)
The motivation for the PR is initially to allow the use [`product_distribution`](https://juliastats.org/Distributions.jl/stable/multivariate/#Distributions.product_distribution) in JuliaBUGS model. By writing ```julia @bugs begin x[1:2] ~ Distributions.product_distribution(fill(Normal(), 2)) end ``` It also enables ```julia julia> foo(x) = x + 1 foo (generic function with 1 method) julia> model_def = @bugs begin a = Main.foo(b) end quote a = Main.foo(b) end julia> compile(model_def, (;b=2)) BUGSModel (parameters are in transformed (unconstrained) space, with dimension 0): Model parameters: Variable sizes and types: a: type = Int64 b: type = Int64 ``` i.e., an easier way to introduce external functions into JuliaBUGS. This can't fully replace `@register_primitive` yet, because using `Main` module is relying on Julia runtime behavior and not very intuitive. Examples: ```julia julia> module TestModule bar(x) = x + 1 end Main.TestModule julia> model_def = @bugs begin a = TestModule.bar(b) end quote a = TestModule.bar(b) end julia> compile(model_def, (; b = 1)) ERROR: UndefVarError: `TestModule` not defined in `JuliaBUGS` ... ``` one would need to do ```julia julia> model_def = @bugs begin a = Main.TestModule.bar(b) end quote a = Main.TestModule.bar(b) end ``` also ```julia julia> @testset "t" begin foo1(x) = x + 1 model_def = @bugs begin a = Main.foo1(b) end compile(model_def, (; b= 1)) end t: Error During Test at REPL[18]:1 Got exception outside of a @test UndefVarError: `foo1` not defined in `Main` ... ```
1 parent 41620db commit 26ef662

File tree

3 files changed

+11
-1
lines changed

3 files changed

+11
-1
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "JuliaBUGS"
22
uuid = "ba9fb4c0-828e-4473-b6a1-cd2560fee5bf"
3-
version = "0.6.5"
3+
version = "0.7.0"
44

55
[deps]
66
AbstractMCMC = "80f14c24-f653-4e6a-9b94-39d6b0f70001"

src/parser/bugs_macro.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,8 @@ function bugs_expression(expr, line_num)
151151
error(
152152
"Keyword argument syntax is not supported in BUGS, error at $line_num: $(expr)"
153153
)
154+
elseif Meta.isexpr(expr, :.)
155+
return expr
154156
else
155157
error("Invalid expression at $line_num: `$expr`")
156158
end

test/compile.jl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,3 +87,11 @@ end
8787
@test AbstractPPL.get(model_init_1.evaluation_env, @varname(beta)) == 1
8888
end
8989
end
90+
91+
@testset "dot call" begin
92+
model_def = @bugs begin
93+
x[1:2] ~ Distributions.product_distribution(fill(Distributions.Normal(0, 1), 2))
94+
end
95+
model = compile(model_def, (;))
96+
@test model.evaluation_env.x isa Vector{Float64}
97+
end

0 commit comments

Comments
 (0)