feat: inner_dist peel hook — collapse leaf-wrapper forwarding boilerplate - #362
feat: inner_dist peel hook — collapse leaf-wrapper forwarding boilerplate#362seabbs-bot wants to merge 2 commits into
Conversation
Add a single-layer peel hook inner_dist(leaf) whose base identity returns the
leaf itself. Every read-through leaf-wrapper hook (free_leaf, uncertain_specs,
extra_leaf_params, shared_tag) now recurses through inner_dist, so a wrapper
type registers ONE method returning its inner distribution instead of one
redundant forwarding method per hook. Register inner_dist for Truncated,
Shared, Uncertain and Varying, and drop their now-redundant explicit forwards.
Rather than the 16 one-line peel methods a wrapper currently needs in the
ModifiedDistributions extension, it now needs 4 inner_dist registrations:
inner_dist(d::Affine) = get_dist(d)
inner_dist(d::Weighted) = get_dist(d)
inner_dist(d::Transformed) = get_dist(d)
inner_dist(d::Modified) = get_dist(d)
The generic peel recurses until the base identity (inner === leaf) stops it,
which is the terminal for a plain leaf. Rebuild hooks (rewrap_leaf,
set_extra_leaf_params, instantiate) and special-cased hooks (has_varying,
Shared's shared_tag, Uncertain's uncertain_specs) stay explicit where their
semantics are not a pure forward. Backward-compatible: existing explicit
wrapper methods still win by dispatch specificity over the new generics.
Closes the leaf-wrapper boilerplate half of the leaf-protocol simplification.
Try this Pull Request!Option 1: Julia Package ManagerOpen Julia and type: import Pkg
Pkg.activate(temp=true)
Pkg.add(url="https://github.com/EpiAware/ComposedDistributions.jl", rev="feat/wrapper-inner-leaf")
using ComposedDistributionsOption 2: Local CheckoutIf you have the repo locally: git checkout feat/wrapper-inner-leaf
julia --project=. -e "using Pkg; Pkg.instantiate()" |
Codecov Report✅ All modified and coverable lines are covered by tests.
Flags with carried forward coverage won't be shown. Click here to find out more.
... and 1 file with indirect coverage changes 🚀 New features to boost your workflow:
|
|
📖 Documentation preview is ready! View the docs for this PR at: https://EpiAware.github.io/ComposedDistributions.jl/previews/PR362/ This preview will be updated automatically when you push new commits. |
Benchmark comparison vs baseMinimum time per call. Buckets are PR time as a % of base, so lower is faster (🟢 faster, ⚪ within 5%, 🔴 slower). Counts of benchmarks per bucket:
Evaluation — 17 benchmarks (by time change)
AD gradients — 36 benchmarks (by time change)
|
|
Superseded by #381, which folds this and seven other PRs into one branch off This branch is preserved and the PR body is kept for its rationale — the detail here is the record of why, which #381 summarises rather than repeats. Closing so it does not compete for review. This comment was posted by a bot. Please ping @seabbs for any questions. |
Adds a single-layer peel hook
inner_dist(leaf)whose base identity returns the leaf itself. The read-through leaf-wrapper hooks (free_leaf,uncertain_specs,extra_leaf_params,shared_tag) now recurse throughinner_dist, so a wrapper type registers ONE method returning its inner distribution instead of one redundant forwarding method per hook.What changes
inner_dist(leaf) = leafbase (a plain leaf's inner is itself — the terminal) plusinner_dist(::Truncated),::Shared,::Uncertain,::Varying.@inlinerecursive peels forfree_leaf/uncertain_specs/extra_leaf_params/shared_tag.free_leaf(d::Shared)etc.).rewrap_leaf,set_extra_leaf_params,instantiate) and genuinely special-cased hooks (has_varying,Shared'sshared_tag,Uncertain'suncertain_specs) stay explicit — they are not pure forwards.The payoff (ModifiedDistributions extension)
Previously a leaf wrapper needed ~16 one-line peel methods. Now it needs 4 registrations:
and every read-through hook forwards through them automatically.
Safety
inner_distand the===check are inferred away for a concrete non-wrapper leaf, sofree_leaf(::Gamma)compiles to an identity. Verified by running the package:uncertain_specs/shared_tagspecific methods still win, nested wrappers peel correctly, plain leaves are identity.@generatedflat codec (codec_gen.jl) uses the type-level registry, not these hooks — unaffected.@testitemcovers plain/shared/truncated/nested behavior.Note: the pre-existing 15 failures + 3 errors in the non-AD suite are in ConvolvedDistributions interop tests (
convolve_seriesauto-discretisation #226,quantile_by_optimization#112/#337) caused by therev=mainConvolvedDistributions pin — addressed separately by PR #359. None touch this change.This was opened by a bot. Please ping @seabbs for any questions.
Refs #332. Unblocks EpiAware/ModifiedDistributions.jl#144.