Skip to content

Named functors from the constraint inspection accessors - #535

Merged
ocots merged 5 commits into
mainfrom
fix/533-named-inspection-callables
Nov 22, 2026
Merged

ocots merged 5 commits into
mainfrom
fix/533-named-inspection-callables

Conversation

@ocots

@ocots ocots commented Nov 22, 2026

Copy link
Copy Markdown
Member

Closes #533.

Conventions: ct-conventions v0.14.0 · ct-agents v0.5.0

What

Every callable returned by the constraint inspection accessors is now a named functor with a stable show. Values and call signatures are unchanged.

julia> CTModels.constraint(ocp, :p)
(:path, SubPathConstraint(n=1, indices=1), 0.0, 1.0)

julia> CTModels.constraint(ocp, :bc)
(:boundary, SubBoundaryConstraint(n=2, indices=[1, 2]), [0.0, 0.0], [0.0, 0.0])

julia> CTModels.path_constraints_nl(ocp)
([0.0], CompositeConstraint{:path}(n=1, dims=[1]), [1.0], [:p])

Design choices (departing from the issue's suggestions where noted)

  1. constraint(model, label): rather than a named wrapper around CTBase.Core.to_out_of_place (or a CTBase change), SubPathConstraint / SubBoundaryConstraint are made out of place and returned directly. CTModels was the only user of to_out_of_place and of these functors in the ecosystem. A single row is stored as an Int, so the scalar/vector shape (DIM1) is encoded in the type, like BoxProjection. The buffer element type is widened from the arguments, as before (AD).
  2. *_constraints_nl: every group is a CompositeConstraint (n = 0, 1, …). The point is less the display than a group type independent of the cardinality. CTDirect already goes through this path as soon as there are two constraints; a performance test checks that a single-declaration group allocates exactly what the wrapped function allocates.
  3. dynamics / lagrange / mayer: not wrapped. They return the user's own functions, which is the right contract (identity preserved). The var"#fun##…" names come from the __symgen(:fun) of CTParser's @def; that belongs to CTParser (separate issue).

Commits (one per phase)

  • fix: out-of-place named functors from constraint (+ docstrings of the functors and of constraint)
  • fix: every nonlinear group is a CompositeConstraint
  • test: regression — no inspection callable prints a var"#" chain, for the five constraint kinds
  • docs: Constraints page (stacked groups, new "Reading a constraint by label" section), CHANGELOG, CHG4 note in BREAKING.md

Checks

  • Full test suite: 5390 / 5390 pass (incl. code quality and performance).
  • Docs build locally (julia --project=docs docs/make.jl, second pass for the InterLinks self-inventory).

Compatibility

No breaking change: the concrete types returned change, not the calls nor their values. Fits a patch release (0.21.3).

🤖 Generated with Claude Code

ocots and others added 4 commits November 22, 2026 17:55
SubPathConstraint and SubBoundaryConstraint become out of place and are
returned directly, instead of being wrapped in the anonymous closure of
CTBase.Core.to_out_of_place, whose display leaked a var"#f#" chain.
A single row is an Int index, so the value is a scalar (DIM1). Refs #533.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A group holding a single declaration stored the raw user function, so the
type and display of path_constraints_nl / boundary_constraints_nl depended
on the number of constraints. Refs #533.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Refs #533.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Constraints page, CompositeConstraint docstring, CHANGELOG and a CHG4 note
in BREAKING.md. Refs #533.

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

ocots commented Nov 22, 2026

Copy link
Copy Markdown
Member Author

Phase A — constraint(model, label) (cc66d08)

SubPathConstraint{F,I} / SubBoundaryConstraint{F,I} are now out of place: f(t, x, u, v) / f(x0, xf, v) allocate a buffer of length n, call the stacked f! and return r[indices]. They store the stacked function only (field f!), not the whole (lb, f, ub, labels) tuple. constraint returns them directly; with rows = Int for a single row, the bounds become cp[1][rows] too, removing the duplicated length(indices) == 1 ? … : … branches.

The buffer element type mirrors to_out_of_place: Float64 widened with the arguments' element types (private helper _buffer_eltype), so AD and the existing ComplexF64 regression test keep working. using CTBase: Core in Models became unused and is dropped.

Tests: scalar/vector rows, BigFloat widening, scalar state/control, @inferred on both shapes, exact repr.

@ocots

ocots commented Nov 22, 2026

Copy link
Copy Markdown
Member Author

Phase B — uniform nonlinear groups (4651141)

build now always stores CompositeConstraint{:path}(n, dims, Tuple(funs)) (same for :boundary), including n = 0 and n = 1; the single-function make_*_cons_nl methods and their @assert are gone.

Hot path check: CTDirect calls path_constraints_nl(ocp)[2] at every grid node. For n = 1 this adds a @view and a one-iteration loop. test_performance.jl now asserts that a single-declaration group allocates exactly what the wrapped function allocates (@ballocated, PERF). Integration tests in test_constraints.jl cover 0, 1 and 2 declarations (type, n, dims, values).

@ocots

ocots commented Nov 22, 2026

Copy link
Copy Markdown
Member Author

Phase C — display regression test (8b0e39d)

New test/suite/display/test_inspection_callables.jl: a model whose constraints are all anonymous closures (single-declaration path and boundary groups, one box per slot). For the five kinds it checks the functor type, the exact compact repr, and that neither repr nor text/plain contains var"#; same for the *_constraints_nl tuples. This is the acceptance criterion of #533 turned into a test; it fails on main.

@ocots

ocots commented Nov 22, 2026

Copy link
Copy Markdown
Member Author

Phase D — docs (3ec23a7)

  • docs/src/model/constraints.md: after the raw tuples, a paragraph on the stacked in-place f! (always a CompositeConstraint, n / dims); a new Reading a constraint by label section with @repl output for :p, :bc, :x1, :vbox, a type → functor → call table, and an evaluation showing the scalar case.
  • Docstrings (DOC2, DOC6): both functors (fields, example) and their call operators, constraint (table of returned functors, # Throws), CompositeConstraint (always used, any cardinality).
  • CHANGELOG.md [Unreleased]: 🛠 Enhancements, 📚 Documentation, ✅ no breaking change. BREAKING.md: CHG4 non-breaking note on the concrete types returned.
  • Side fix: two French section headers ("Famille E/F") replaced (GEN1).

Resolve the [Unreleased] conflict of CHANGELOG.md: keep the entries of #526
and #533, in the CHG2 section order, with one Compatibility footer.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@ocots
ocots marked this pull request as ready for review November 22, 2026 18:47
@ocots
ocots merged commit c779286 into main Nov 22, 2026
3 checks passed
@ocots
ocots deleted the fix/533-named-inspection-callables branch November 22, 2026 18:47
@github-actions github-actions Bot mentioned this pull request Nov 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Harmonize the printed representation of functions returned by inspection accessors

1 participant