@@ -5,6 +5,7 @@ using ..DynamicPPL: broadcast_safe, AbstractContext, childcontext
55
66using Random: Random
77using Accessors: Accessors
8+ using InteractiveUtils: InteractiveUtils
89
910using DocStringExtensions
1011using Distributions
@@ -331,7 +332,9 @@ function DynamicPPL.tilde_assume(context::DebugContext, right, vn, vi)
331332 record_post_tilde_assume! (context, vn, right, value, logp, vi)
332333 return value, logp, vi
333334end
334- function DynamicPPL. tilde_assume (rng, context:: DebugContext , sampler, right, vn, vi)
335+ function DynamicPPL. tilde_assume (
336+ rng:: Random.AbstractRNG , context:: DebugContext , sampler, right, vn, vi
337+ )
335338 record_pre_tilde_assume! (context, vn, right, vi)
336339 value, logp, vi = DynamicPPL. tilde_assume (
337340 rng, childcontext (context), sampler, right, vn, vi
@@ -424,7 +427,7 @@ function DynamicPPL.dot_tilde_assume(context::DebugContext, right, left, vn, vi)
424427end
425428
426429function DynamicPPL. dot_tilde_assume (
427- rng, context:: DebugContext , sampler, right, left, vn, vi
430+ rng:: Random.AbstractRNG , context:: DebugContext , sampler, right, left, vn, vi
428431)
429432 record_pre_dot_tilde_assume! (context, vn, left, right, vi)
430433 value, logp, vi = DynamicPPL. dot_tilde_assume (
@@ -678,4 +681,83 @@ function has_static_constraints(
678681 return all_the_same (transforms)
679682end
680683
684+ """
685+ gen_evaluator_call_with_types(model[, varinfo, context])
686+
687+ Generate the evaluator call and the types of the arguments.
688+
689+ # Arguments
690+ - `model::Model`: The model whose evaluator is of interest.
691+ - `varinfo::AbstractVarInfo`: The varinfo to use when evaluating the model. Default: `VarInfo(model)`.
692+ - `context::AbstractContext`: The context to use when evaluating the model. Default: [`DefaultContext`](@ref).
693+
694+ # Returns
695+ A 2-tuple with the following elements:
696+ - `f`: This is either `model.f` or `Core.kwcall`, depending on whether
697+ the model has keyword arguments.
698+ - `argtypes::Type{<:Tuple}`: The types of the arguments for the evaluator.
699+ """
700+ function gen_evaluator_call_with_types (
701+ model:: Model ,
702+ varinfo:: AbstractVarInfo = VarInfo (model),
703+ context:: AbstractContext = DefaultContext (),
704+ )
705+ args, kwargs = DynamicPPL. make_evaluate_args_and_kwargs (model, varinfo, context)
706+ return if isempty (kwargs)
707+ (model. f, Base. typesof (args... ))
708+ else
709+ (Core. kwcall, Tuple{typeof (kwargs),Core. Typeof (model. f),map (Core. Typeof, args)... })
710+ end
711+ end
712+
713+ """
714+ model_warntype(model[, varinfo, context]; optimize=true)
715+
716+ Check the type stability of the model's evaluator, warning about any potential issues.
717+
718+ This simply calls `@code_warntype` on the model's evaluator, filling in internal arguments where needed.
719+
720+ # Arguments
721+ - `model::Model`: The model to check.
722+ - `varinfo::AbstractVarInfo`: The varinfo to use when evaluating the model. Default: `VarInfo(model)`.
723+ - `context::AbstractContext`: The context to use when evaluating the model. Default: [`DefaultContext`](@ref).
724+
725+ # Keyword Arguments
726+ - `optimize::Bool`: Whether to generate optimized code. Default: `false`.
727+ """
728+ function model_warntype (
729+ model:: Model ,
730+ varinfo:: AbstractVarInfo = VarInfo (model),
731+ context:: AbstractContext = DefaultContext ();
732+ optimize:: Bool = false ,
733+ )
734+ ftype, argtypes = gen_evaluator_call_with_types (model, varinfo, context)
735+ return InteractiveUtils. code_warntype (ftype, argtypes; optimize= optimize)
736+ end
737+
738+ """
739+ model_typed(model[, varinfo, context]; optimize=true)
740+
741+ Return the type inference for the model's evaluator.
742+
743+ This simply calls `@code_typed` on the model's evaluator, filling in internal arguments where needed.
744+
745+ # Arguments
746+ - `model::Model`: The model to check.
747+ - `varinfo::AbstractVarInfo`: The varinfo to use when evaluating the model. Default: `VarInfo(model)`.
748+ - `context::AbstractContext`: The context to use when evaluating the model. Default: [`DefaultContext`](@ref).
749+
750+ # Keyword Arguments
751+ - `optimize::Bool`: Whether to generate optimized code. Default: `true`.
752+ """
753+ function model_typed (
754+ model:: Model ,
755+ varinfo:: AbstractVarInfo = VarInfo (model),
756+ context:: AbstractContext = DefaultContext ();
757+ optimize:: Bool = true ,
758+ )
759+ ftype, argtypes = gen_evaluator_call_with_types (model, varinfo, context)
760+ return only (InteractiveUtils. code_typed (ftype, argtypes; optimize= optimize))
761+ end
762+
681763end
0 commit comments