From 8c8bb4fcbda22722c7c7467ae092429ab8a53e21 Mon Sep 17 00:00:00 2001 From: Hong Ge Date: Thu, 19 Nov 2026 13:43:54 +0000 Subject: [PATCH] varnamedtuple: describe storage with `AbstractPPL.of` `of(vnt)` describes a VarNamedTuple's storage as an AbstractPPL template. Nothing consumes it yet: the binding API added in a later change accepts such templates in `condition` and `fix`. Assisted-by: Claude Code Assisted-by: Codex --- HISTORY.md | 2 ++ Project.toml | 2 +- src/varnamedtuple.jl | 50 +++++++++++++++++++++++++++++++++++++++++++ test/varnamedtuple.jl | 10 +++++++++ 4 files changed, 63 insertions(+), 1 deletion(-) diff --git a/HISTORY.md b/HISTORY.md index b64606f55..fae8df2fd 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -1,5 +1,7 @@ # 0.43.0 (unreleased) +Added `AbstractPPL.of(vnt::VarNamedTuple)`, which describes a `VarNamedTuple`'s storage as an AbstractPPL binding template. It supports numeric `Array`s, nested fields and scalars whose type `of` preserves; other storage, such as values set by index without a template, throws an `ArgumentError` ([#1547](https://github.com/TuringLang/DynamicPPL.jl/pull/1547)). + When rebuilding a `VarNamedTuple` from its skeleton, each nested array now keeps its original size. Previously, reusing the first array’s size could produce incorrectly sized arrays or throw a `BoundsError` ([#1540](https://github.com/TuringLang/DynamicPPL.jl/issues/1540)). `VarNamedTuple` records indexed random variables in same-type tuple templates ([#1539](https://github.com/TuringLang/DynamicPPL.jl/pull/1539)). diff --git a/Project.toml b/Project.toml index 004a775ee..d704c2e35 100644 --- a/Project.toml +++ b/Project.toml @@ -54,7 +54,7 @@ DynamicPPLReverseDiffExt = ["ReverseDiff"] [compat] ADTypes = "1" AbstractMCMC = "5.17" -AbstractPPL = "0.15" +AbstractPPL = "0.15.3" Accessors = "0.1" BangBang = "0.4.1" Bijectors = "0.16" diff --git a/src/varnamedtuple.jl b/src/varnamedtuple.jl index c53cf004d..ec8f9f2d4 100644 --- a/src/varnamedtuple.jl +++ b/src/varnamedtuple.jl @@ -140,6 +140,56 @@ include("varnamedtuple/show.jl") include("varnamedtuple/macro.jl") include("varnamedtuple/skeleton.jl") +""" + AbstractPPL.of(vnt::VarNamedTuple) + +Describe the storage in `vnt` as an AbstractPPL `OfNamedTuple` binding template. + +Numeric `Array`s keep their element type and full size, even when partly filled; nested +namespaces become nested templates; real scalars are kept when `of` describes their type +exactly. Values are discarded. Other storage, such as custom or growable arrays, indexed +namespaces and scalars that `of` would widen, throws `ArgumentError`. + +# Examples + +```jldoctest +julia> using AbstractPPL: of + +julia> template = of(VarNamedTuple(; y=zeros(Float32, 2))); + +julia> zero(template) +(y = Float32[0.0, 0.0],) +``` +""" +AbstractPPL.of(vnt::VarNamedTuple) = _storage_template(vnt, "") + +# `path` is the dotted field path of the stored value, for error messages. +_storage_template(vnt::VarNamedTuple, path) = _storage_template(vnt.data, path) +function _storage_template(nt::NamedTuple{names}, path) where {names} + child(k) = isempty(path) ? string(k) : "$path.$k" + return AbstractPPL.of( + NamedTuple{names}(map(k -> _storage_template(nt[k], child(k)), names)) + ) +end +_storage_template(pa::PartialArray, path) = _storage_template(pa.data, path) +_storage_template(a::Array{<:Number}, _) = AbstractPPL.of(a) +function _storage_template(x::Real, path) + template = AbstractPPL.of(x) + typeof(zero(template)) === typeof(x) || throw( + ArgumentError( + "Cannot preserve scalar type $(typeof(x)) of `$path` in a binding template: `of` would widen it to $(typeof(zero(template))).", + ), + ) + return template +end +function _storage_template(x, path) + throw( + ArgumentError( + "Cannot describe `$path` of type $(typeof(x)) exactly in a binding template." + ), + ) +end + """ NamedTuple(vnt::VarNamedTuple) diff --git a/test/varnamedtuple.jl b/test/varnamedtuple.jl index 3430f401b..6f230e215 100644 --- a/test/varnamedtuple.jl +++ b/test/varnamedtuple.jl @@ -2565,6 +2565,16 @@ end end end +@testset "binding templates from storage" begin + pa = templated_setindex!!(VarNamedTuple(), 1.0f0, @varname(x[2]), zeros(Float32, 3)) + @test AbstractPPL.of(VarNamedTuple(; a=pa, b=1.0)) == + AbstractPPL.of((a=(x=zeros(Float32, 3),), b=1.0)) + @test_throws "`a.x` of type String" AbstractPPL.of( + VarNamedTuple(; a=VarNamedTuple(; x="a")) + ) + @test_throws "would widen it to Float64" AbstractPPL.of(VarNamedTuple(; x=1//2)) +end + @info "Completed $(@__FILE__) in $(now() - __now__)." end