API Reference
Model Definition
StanBlocks.@slic Macro
Defines SlicModels (see test/slic.jl for usage examples).
The defining module is captured automatically via __module__, so that @deffun functions defined in the same module (e.g. a package extension) are found during symbol resolution.
StanBlocks.@deffun Macro
@deffun function_definitionDefine a Stan-compatible function with type inference and code generation.
Parses a Julia-style function definition (with type-annotated arguments and return type), generates the corresponding Stan function, and registers type-inference signatures so the transpiler can propagate types through calls to this function.
For functions ending in _lpdf/_lpmf/_lcdf/_lccdf, the return type is automatically set to real and companion _lpdfs/_rng stubs are generated for use in generated_quantities.
Example
@deffun garch11_lpdf(y::vector[T], mu::real, alpha0::real, alpha1::real, beta1::real)::real = begin
sigma2 = alpha0
rv = 0.
for t in 1:T
rv += normal_lpdf(y[t], mu, sqrt(sigma2))
sigma2 = alpha0 + alpha1 * square(y[t] - mu) + beta1 * sigma2
end
return rv
endSee src/slic_stan/builtin.jl for many more examples.
StanBlocks.@defsig Macro
Utility macro to define function signatures (see src/slic_stan/builtin.jl for usage examples).
Note:
This macro is mainly useful for bulk built-in function signature definitions. StanBlocks.jl users should generally prefer using @deffun.
sourceModel Inspection and Compilation
StanBlocks.stan_code Function
Return the stan code of its first argument (a StanBlocks.SlicModel or a StanBlocks.StanModel) as a string.
StanBlocks.stan_model Function
Traces through its first argument (a StanBlocks.SlicModel) and returns the inferred StanBlocks.StanModel.
StanBlocks.stan_instantiate Function
Returns the StanLogDensityProblem (a compiled posterior).
Warning:
Requires loading StanLogDensityProblems.jl and JSON.jl.
sourceTypes
StanBlocks.SlicModel Type
The AST and the data, pre-tracing. Can be instantiated via stan_instantiate.
The mod field stores the defining module (set automatically by @slic), used for symbol resolution during tracing — functions defined via @deffun in package extensions are found by checking mod before falling back to Main.
Warning:
Repeatedly instantiating SlicModels is inefficient, as the tracing is redone for every instantiation. Instead, get the StanModel first (via model = stan_model(slic_model)) and update its data (via new_model = model(;x=new_x)).
StanBlocks.StanModel Type
The inferred Stan model, post-tracing. Can be instantiated via stan_instantiate.
Errors
StanBlocks.StanBlocksError Type
StanBlocksError <: ExceptionWraps errors that occur during transpilation, compilation, or evaluation of Stan models.
Fields
phase::Symbol: the pipeline stage where the error occurred (:transpile,:compile, or:evaluate)context::String: a description of what was being processed (e.g."model: eight_schools")cause::Any: the underlying error, typically an(exception, backtrace)tuple