@@ -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
@@ -678,4 +679,83 @@ function has_static_constraints(
678679 return all_the_same (transforms)
679680end
680681
682+ """
683+ gen_evaluator_call_with_types(model[, varinfo, context])
684+
685+ Generate the evaluator call and the types of the arguments.
686+
687+ # Arguments
688+ - `model::Model`: The model whose evaluator is of interest.
689+ - `varinfo::AbstractVarInfo`: The varinfo to use when evaluating the model. Default: `VarInfo(model)`.
690+ - `context::AbstractContext`: The context to use when evaluating the model. Default: [`DefaultContext`](@ref).
691+
692+ # Returns
693+ A 2-tuple with the following elements:
694+ - `f`: This is either `model.f` or `Core.kwcall`, depending on whether
695+ the model has keyword arguments.
696+ - `argtypes::Type{<:Tuple}`: The types of the arguments for the evaluator.
697+ """
698+ function gen_evaluator_call_with_types (
699+ model:: Model ,
700+ varinfo:: AbstractVarInfo = VarInfo (model),
701+ context:: AbstractContext = DefaultContext (),
702+ )
703+ args, kwargs = DynamicPPL. make_evaluate_args_and_kwargs (model, varinfo, context)
704+ return if isempty (kwargs)
705+ (model. f, Base. typesof (args... ))
706+ else
707+ (Core. kwcall, Tuple{typeof (kwargs),Core. Typeof (model. f),map (Core. Typeof, args)... })
708+ end
709+ end
710+
711+ """
712+ model_warntype(model[, varinfo, context]; optimize=true)
713+
714+ Check the type stability of the model's evaluator, warning about any potential issues.
715+
716+ This simply calls `@code_warntype` on the model's evaluator, filling in internal arguments where needed.
717+
718+ # Arguments
719+ - `model::Model`: The model to check.
720+ - `varinfo::AbstractVarInfo`: The varinfo to use when evaluating the model. Default: `VarInfo(model)`.
721+ - `context::AbstractContext`: The context to use when evaluating the model. Default: [`DefaultContext`](@ref).
722+
723+ # Keyword Arguments
724+ - `optimize::Bool`: Whether to generate optimized code. Default: `false`.
725+ """
726+ function model_warntype (
727+ model:: Model ,
728+ varinfo:: AbstractVarInfo = VarInfo (model),
729+ context:: AbstractContext = DefaultContext ();
730+ optimize:: Bool = false ,
731+ )
732+ ftype, argtypes = gen_evaluator_call_with_types (model, varinfo, context)
733+ return InteractiveUtils. code_warntype (ftype, argtypes; optimize= optimize)
734+ end
735+
736+ """
737+ model_typed(model[, varinfo, context]; optimize=true)
738+
739+ Return the type inference for the model's evaluator.
740+
741+ This simply calls `@code_typed` on the model's evaluator, filling in internal arguments where needed.
742+
743+ # Arguments
744+ - `model::Model`: The model to check.
745+ - `varinfo::AbstractVarInfo`: The varinfo to use when evaluating the model. Default: `VarInfo(model)`.
746+ - `context::AbstractContext`: The context to use when evaluating the model. Default: [`DefaultContext`](@ref).
747+
748+ # Keyword Arguments
749+ - `optimize::Bool`: Whether to generate optimized code. Default: `true`.
750+ """
751+ function model_typed (
752+ model:: Model ,
753+ varinfo:: AbstractVarInfo = VarInfo (model),
754+ context:: AbstractContext = DefaultContext ();
755+ optimize:: Bool = true ,
756+ )
757+ ftype, argtypes = gen_evaluator_call_with_types (model, varinfo, context)
758+ return only (InteractiveUtils. code_typed (ftype, argtypes; optimize= optimize))
759+ end
760+
681761end
0 commit comments