@@ -133,8 +133,7 @@ function logprior(
133133 # All `observe` and `dot_observe` calls are no-op in the PriorContext
134134
135135 # When all of model args are on the lhs of |, this is also equal to the logjoint.
136- args, missings = get_prior_model_args (left, right, modeltype)
137- model = Model {G, missings} (args)
136+ model = make_prior_model (left, right, modeltype)
138137 vi = _vi === nothing ? VarInfo (deepcopy (model), PriorContext ()) : _vi
139138 foreach (keys (vi. metadata)) do n
140139 @assert n in keys (left) " Variable $n is not defined."
@@ -143,35 +142,35 @@ function logprior(
143142 return getlogp (vi)
144143end
145144
146- @generated function get_prior_model_args (
145+ @generated function make_prior_model (
147146 left:: NamedTuple{leftnames} ,
148147 right:: NamedTuple{rightnames} ,
149- modeltype:: Type{<:Model{_G , argnames}} ,
148+ modeltype:: Type{<:Model{G , argnames}} ,
150149 defaults:: NamedTuple{defaultnames} = getdefaults (modeltype)
151- ) where {leftnames, rightnames, argnames, defaultnames, _G }
152- args = []
150+ ) where {leftnames, rightnames, G, argnames, defaultnames }
151+ argvals = []
153152 missings = []
154153 warnings = []
155154
156155 for argname in argnames
157156 if argname in leftnames
158- push! (args , :($ argname = deepcopy (left.$ argname)))
157+ push! (argvals , :(deepcopy (left.$ argname)))
159158 push! (missings, argname)
160159 elseif argname in rightnames
161- push! (args , :($ argname = right.$ argname))
160+ push! (argvals , :(right.$ argname))
162161 elseif argname in defaultnames
163- push! (args , :($ argname = defaults.$ argname))
162+ push! (argvals , :(defaults.$ argname))
164163 else
165164 push! (warnings, :(@warn ($ (warn_msg (argname)))))
166- push! (args , :($ argname = nothing ))
165+ push! (argvals , :(nothing ))
167166 end
168167 end
169168
170- # `args` is spatted as a NamedTuple expression; `missings` is splatted into a tuple and
171- # inserted as literal
169+ # `args` is inserted as properly typed NamedTuple expression;
170+ # `missings` is splatted into a tuple at compile time and inserted as literal
172171 return quote
173172 $ (warnings... )
174- ((; $ (args ... ))) , $ ((missings... ,))
173+ DynamicPPL . Model {G , $((missings...,))} ( $ ( to_namedtuple_expr (argnames, argvals) ))
175174 end
176175end
177176
@@ -183,9 +182,7 @@ function loglikelihood(
183182 modeltype:: Type{<:Model{G}} ,
184183 _vi:: Union{Nothing, VarInfo} ,
185184) where {G}
186- # Pass namesl to model constructor, remaining args are missing
187- args, missings = get_like_model_args (left, right, modeltype)
188- model = Model {G, missings} (args)
185+ model = make_likelihood_model (left, right, modeltype)
189186 vi = _vi === nothing ? VarInfo (deepcopy (model)) : _vi
190187 if isdefined (right, :chain )
191188 # Element-wise likelihood for each value in chain
@@ -206,31 +203,31 @@ function loglikelihood(
206203 end
207204end
208205
209- @generated function get_like_model_args (
206+ @generated function make_likelihood_model (
210207 left:: NamedTuple{leftnames} ,
211208 right:: NamedTuple{rightnames} ,
212- modeltype:: Type{<:Model{_G , argnames}} ,
209+ modeltype:: Type{<:Model{G , argnames}} ,
213210 defaults:: NamedTuple{defaultnames} = getdefaults (modeltype)
214- ) where {leftnames, rightnames, argnames, defaultnames, _G }
215- args = []
211+ ) where {leftnames, rightnames, G, argnames, defaultnames }
212+ argvals = []
216213 missings = []
217214
218215 for argname in argnames
219216 if argname in leftnames
220- push! (args , :($ argname = left.$ argname))
217+ push! (argvals , :(left.$ argname))
221218 elseif argname in rightnames
222- push! (args , :($ argname = right.$ argname))
219+ push! (argvals , :(right.$ argname))
223220 push! (missings, argname)
224221 elseif argname in defaultnames
225- push! (args , :($ argname = defaults.$ argname))
222+ push! (argvals , :(defaults.$ argname))
226223 else
227224 throw (" This point should not be reached. Please open an issue in the DynamicPPL.jl repository." )
228225 end
229226 end
230227
231- # `args` is spatted as a NamedTuple expression; `missings` is splatted into a tuple and
232- # inserted as literal
233- return :((; $ (args ... )) , $ ((missings... ,)))
228+ # `args` is inserted as properly typed NamedTuple expression;
229+ # `missings` is splatted into a tuple at compile time and inserted as literal
230+ return :(DynamicPPL . Model {G , $((missings...,))} ( $ ( to_namedtuple_expr (argnames, argvals) )))
234231end
235232
236233_setval! (vi:: TypedVarInfo , c:: AbstractChains ) = _setval! (vi. metadata, vi, c)
0 commit comments