Skip to content

Public trait joins; AbstractConstrainedPseudoHamiltonian as an extension point - #667

Merged
ocots merged 4 commits into
mainfrom
feat/666-constrained-extension-trait-join
Nov 24, 2026
Merged

ocots merged 4 commits into
mainfrom
feat/666-constrained-extension-trait-join

Conversation

@ocots

@ocots ocots commented Nov 24, 2026 •

Copy link
Copy Markdown
Member

Closes #666. Follow-up of #663, from the review in control-toolbox/CTModels.jl#536.

Conventions: ct-conventions v0.15.0, ct-agents v0.5.0.

Two changes to make before CTBase 0.30.15 is released, while the API is not public yet.

Phases

  • A. Public trait joins: Traits.join_time_dependence and Traits.join_variable_dependence replace the private Data._join_td and Data._join_vd.
  • B. AbstractConstrainedPseudoHamiltonian as an extension point: freeze_multiplier and ComposedConstrainedHamiltonian accept any subtype that implements the call contract (TYP5), as Differentiation already does for AbstractHamiltonian and AbstractPseudoHamiltonian.
  • C. Docs, docstrings, CHANGELOG.md, BREAKING.md (a CHG4 note for the removed private helpers).

Each phase is one commit with an explanatory comment.

Public API

  • Additive only.
  • New: Traits.join_time_dependence and Traits.join_variable_dependence.
  • Widened upper bound: freeze_multiplier and ComposedConstrainedHamiltonian now take any AbstractConstrainedPseudoHamiltonian (phase B).
  • Removed: the private Data._join_td and Data._join_vd. Nothing in the ecosystem uses them; I checked CTModels, CTFlows, CTDirect, CTSolvers, CTParser and OptimalControl.

Out of scope

🤖 Generated with Claude Code

…dependence

Traits.join_time_dependence and Traits.join_variable_dependence join one or
more trait values (Autonomous / Fixed only if all are). They replace the
private Data._join_td and Data._join_vd, so that CTModels and CTFlows can
reuse them instead of re-implementing them (#666).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@ocots

ocots commented Nov 24, 2026

Copy link
Copy Markdown
Member Author

Phase A — public trait joins (6dfefaf)

API

Traits.join_time_dependence(Traits.Autonomous, Traits.NonAutonomous)          # NonAutonomous
Traits.join_variable_dependence(Traits.Fixed, Traits.Fixed, Traits.Fixed)     # Fixed
  • Inputs. The functions take trait values (types), one or more of them.
  • Result. Autonomous (respectively Fixed) only if every argument is.
  • Three or more arguments. They fold left. ComposedConstrainedHamiltonian now joins its three inputs in one call, and CTModels can write join_time_dependence(map(time_dependence, μs)...).
  • Type stability. The result is folded to a constant: @inferred holds for 2 and 4 arguments.
  • Errors. A trait value of the other family, or no argument at all, is a MethodError.

Implementation

  • The functions are in src/Traits/time_dependence.jl and src/Traits/variable_dependence.jl, and are exported by Traits.
  • The private Data._join_td and Data._join_vd are deleted. ComposedHamiltonian, ComposedVectorField and ComposedConstrainedHamiltonian call the public functions.

Tests

The tests are in test_time_dependence.jl and test_variable_dependence.jl:

  • the full truth table, with 1 to 4 arguments;
  • @inferred;
  • a value of the other trait family, and no argument;
  • the exports, also checked in test_traits_module.jl.

The existing tests of the composed types (traits joined) still pass, as non-regression.

Checks

  • suite/traits and suite/data: 1399/1399.
  • suite/meta: 73/74. The only failure also happens on main: test_extensions_quality finds no CTBaseMooncake module (nothing isa Module, ext/CTBaseAqua.jl:133). It is unrelated to this PR and should be handled separately.

Next: phase B, AbstractConstrainedPseudoHamiltonian as an extension point.

freeze_multiplier and ComposedConstrainedHamiltonian accept any subtype of
AbstractConstrainedPseudoHamiltonian that implements the call contract
(natural and uniform calls), not only the concrete ConstrainedPseudoHamiltonian
(TYP5), as Differentiation already does for the other abstract Hamiltonians.
Contract tests with a top-level fake: values, traits, type stability and AD
gradients equal to those of the concrete type (#666).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@ocots

ocots commented Nov 24, 2026

Copy link
Copy Markdown
Member Author

Phase B — AbstractConstrainedPseudoHamiltonian as an extension point (52b17c1)

What changes

Signature Before Now
freeze_multiplier(h, μ) h::ConstrainedPseudoHamiltonian h::AbstractConstrainedPseudoHamiltonian
ComposedConstrainedHamiltonian(h, law, μ) h::ConstrainedPseudoHamiltonian h::AbstractConstrainedPseudoHamiltonian
type parameter PH of ComposedConstrainedHamiltonian <:ConstrainedPseudoHamiltonian <:AbstractConstrainedPseudoHamiltonian
  • Why. These two functions only use the call contract of the abstract type: the natural call of its traits and the uniform call (t, x, p, u, μ, v). A package can now subtype AbstractConstrainedPseudoHamiltonian directly (TYP5), as Differentiation already allows for AbstractHamiltonian and AbstractPseudoHamiltonian.
  • Non-breaking. Only upper bounds are widened: every call that worked before still works and gives the same value.
  • The private functor _FrozenMultiplier now dispatches on AbstractConstrainedPseudoHamiltonian{TD,VD}.

Tests

A fake FakeConstrainedPH{TD,VD} <: AbstractConstrainedPseudoHamiltonian{TD,VD} sits at module top level (TST3). It is a callable struct that implements the natural and uniform calls, and nothing else.

  • Contract, freeze_multiplier:
    • the 4 TD×VD combinations: it returns a PseudoHamiltonian with the traits of the fake;
    • natural and uniform calls give the closed form;
    • @inferred.
  • Contract, ComposedConstrainedHamiltonian:
    • the value is the same as with the concrete type for the same function;
    • natural and uniform calls, getter, @inferred;
    • the traits of the fake enter the join (NonAutonomous, NonFixed).
  • AD (test_constrained_hamiltonian_gradient.jl): through the fake, the partial gradients (freeze_multiplier) and the total gradients (composed Hamiltonian) are equal to those through the concrete type.
  • Error: freeze_multiplier still rejects a PseudoHamiltonian and a plain function (MethodError). The existing rejections in ComposedConstrainedHamiltonian are unchanged.

Checks

  • suite/data and suite/differentiation: 1198/1198.
  • suite/meta: 73/74, with the same CTBaseMooncake failure as on main (see phase A).
  • The touched files were formatted with JuliaFormatter (Blue style). This also re-wraps one line of phase A.

Next: phase C, with the docstrings, the guides (traits and data), CHANGELOG.md, the CHG4 note in BREAKING.md, the docs build and the full suite.

Docstrings of Traits.join_time_dependence and join_variable_dependence, the
interface requirements of AbstractConstrainedPseudoHamiltonian, and the
widened signatures. A "Joining traits" section in the Traits guide and an
executed example of a custom subtype in the Data guide. CHANGELOG entries
with the missing Compatibility footer, and a CHG4 note in BREAKING.md for the
removed private helpers.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@ocots

ocots commented Nov 24, 2026 •

Copy link
Copy Markdown
Member Author

Phase C — docs, docstrings, changelog (56b01de)

Docstrings (DOC1–DOC8)

  • Traits.join_time_dependence and Traits.join_variable_dependence: one docstring per method.
    • The one-argument method carries the full one, with arguments, returns and an example on 1, 2 and 3 arguments.
    • The two-argument and n-argument methods have a short one each.
  • AbstractConstrainedPseudoHamiltonian: a new # Interface Requirements section (DOC2).
    • It lists the natural call of the traits and the uniform call (t, x, p, u, μ, v).
    • It says what accepts a subtype: freeze_multiplier, ComposedConstrainedHamiltonian, and the Differentiation gradients through them.
  • freeze_multiplier and ComposedConstrainedHamiltonian: the argument and the type parameter now name the abstract type. "concrete" is removed.
  • The constructors of the three composed types link to the joins.

Guides

  • Traits: a new section, "Joining traits", with:
    • the rule;
    • a table;
    • executed examples, among them the join of the three parts of a composed constrained Hamiltonian.
  • Data: "Extending the abstract type", in the ConstrainedPseudoHamiltonian section.
    • It has an executed example: a callable struct OrderOneH <: AbstractConstrainedPseudoHamiltonian that stores the bound.
    • freeze_multiplier gives on it the same value as on the concrete type.
    • The ComposedConstrainedHamiltonian section now says that it accepts any subtype.

CHANGELOG.md and BREAKING.md

Checks

  • Full suite: 5713/5713.
  • release.jl lint-changelog (CTActions dc60661): [Unreleased] follows CHG2–CHG4.
  • Docs build: two passes for the InterLinks bootstrap. The second pass has no error, and the new examples run.

Not in this PR

All phases A–C are done. The PR stays in draft for your review.

…extension-trait-join

# Conflicts:
#	BREAKING.md
#	CHANGELOG.md
@ocots
ocots marked this pull request as ready for review November 24, 2026 17:11
@ocots ocots added run ci github-runner Trigger the CI workflow on GitHub-hosted runners run documentation Trigger the Documentation workflow on this PR labels Nov 24, 2026
@ocots
ocots merged commit aed019d into main Nov 24, 2026
41 of 51 checks passed
@ocots
ocots deleted the feat/666-constrained-extension-trait-join branch November 24, 2026 17:53
ocots added a commit to control-toolbox/CTModels.jl that referenced this pull request Nov 24, 2026
_stacked_multiplier joins the traits of the multipliers with
CTBase.Traits.join_time_dependence and join_variable_dependence
(control-toolbox/CTBase.jl#667) instead of private copies. [sources] points
to CTBase main, where #663 and #667 are merged; the branch of #663 is deleted.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

run ci github-runner Trigger the CI workflow on GitHub-hosted runners run documentation Trigger the Documentation workflow on this PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Before 0.30.15: extension point of the constrained pseudo-Hamiltonian, public trait join

1 participant