Skip to content

Commit 8193b52

Browse files
committed
Remove CTFlows references and fix docstring compliance in Traits module
- Remove CTFlows.Configs references from trait docstrings and examples - Change @extref to @ref for internal Exceptions symbols - Update documentation (getting-started, guide/traits, index) - Clean up trait examples to remove CTFlows-specific configuration references - Part of CTFlows→CTBase migration
1 parent 6021fea commit 8193b52

9 files changed

Lines changed: 112 additions & 92 deletions

File tree

docs/src/getting-started.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,11 @@ Three things to keep in mind:
3131
CTBase.Exceptions.NotImplemented
3232
```
3333
2. **Submodule-first API.** The public API lives in named submodules
34-
(`Descriptions`, `Exceptions`, `DevTools`, `Core`, `Unicode`).
34+
(`Core`, `Descriptions`, `Exceptions`, `Traits`, `DevTools`, `Unicode`, …).
3535
You can bring a submodule's exports into scope explicitly:
3636
```julia
3737
using CTBase.Exceptions # brings IncorrectArgument, NotImplemented, … into scope
38+
using CTBase.Traits # brings Autonomous, NonAutonomous, is_autonomous, … into scope
3839
```
3940
3. **Extension-backed features.** `run_tests`, `postprocess_coverage`, and
4041
`automatic_reference_documentation` require loading the matching weak dependency
@@ -111,6 +112,7 @@ For more, see the **[Exceptions guide](guide/exceptions.md)**.
111112
| :--- | :--- |
112113
| Descriptions catalogue and completion | [Descriptions](guide/descriptions.md) |
113114
| Exception hierarchy and best practices | [Exceptions](guide/exceptions.md) |
115+
| Compile-time traits and dispatch | [Traits](guide/traits.md) |
114116
| Modular test runner setup | [Test Runner](guide/test-runner.md) |
115117
| Coverage report generation | [Coverage](guide/coverage.md) |
116118
| Auto-generated API reference | [API Documentation](guide/api-documentation.md) |

docs/src/guide/traits.md

Lines changed: 44 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,19 @@ CurrentModule = CTBase
55
```
66

77
[`CTBase.Traits`](@ref CTBase.Traits) provides a small set of **compile-time
8-
traits** shared across the control-toolbox ecosystem. Traits are empty marker
9-
types used as type parameters (or returned by accessor functions) so that
8+
traits** shared across the control-toolbox ecosystem. A trait is an abstract type
9+
used as a **type parameter** or returned by an accessor function so that
1010
behaviour can be selected by dispatch, with **no runtime cost**.
1111

1212
```@repl traits
1313
using CTBase
1414
import CTBase.Traits
1515
```
1616

17-
Downstream packages use these traits to encode, for example, the **call
18-
signature** of a vector field or Hamiltonian, and to dispatch system builders
19-
without runtime branches.
17+
A typical use case is encoding a property of a callable object — does it take a
18+
time argument? does it depend on an extra variable? is it evaluated in-place? —
19+
so that a wrapper type can select the correct call path at compile time, without
20+
runtime conditionals.
2021

2122
## Trait families
2223

@@ -30,8 +31,8 @@ Does the object depend on time ``t``?
3031
| [`Traits.NonAutonomous`](@ref CTBase.Traits.NonAutonomous) | ``t`` must be supplied |
3132

3233
Both are abstract subtypes of [`Traits.TimeDependence`](@ref CTBase.Traits.TimeDependence).
33-
They are used as the time-dependence type parameter of `CTModels.Model` and of
34-
CTFlows data wrappers.
34+
Because they are abstract, they can only appear as type parameters — they are not
35+
instantiated, only dispatched upon.
3536

3637
```@repl traits
3738
Traits.Autonomous <: Traits.TimeDependence
@@ -48,6 +49,8 @@ design variable)?
4849
| [`Traits.Fixed`](@ref CTBase.Traits.Fixed) | no ``v`` argument |
4950
| [`Traits.NonFixed`](@ref CTBase.Traits.NonFixed) | ``v`` must be supplied |
5051

52+
Both are concrete subtypes of [`Traits.VariableDependence`](@ref CTBase.Traits.VariableDependence):
53+
5154
```@repl traits
5255
Traits.Fixed <: Traits.VariableDependence
5356
Traits.NonFixed <: Traits.VariableDependence
@@ -64,6 +67,10 @@ Does a function allocate a new output, or write into a pre-allocated buffer?
6467

6568
### Other families
6669

70+
These traits encode properties that are fixed at construction time and carried
71+
as type parameters. They are not opted into via the two-method contract described
72+
below — they are passed directly as type arguments.
73+
6774
| Family | Values |
6875
|---|---|
6976
| Integration mode | [`Traits.EndPointMode`](@ref CTBase.Traits.EndPointMode), [`Traits.TrajectoryMode`](@ref CTBase.Traits.TrajectoryMode) |
@@ -79,7 +86,7 @@ A type opts in to a trait by implementing **two methods**: one declaring that it
7986
[`Traits.is_variable`](@ref CTBase.Traits.is_variable), …) then follow
8087
generically — they are not implemented per type.
8188

82-
For time dependence, implement `has_time_dependence_trait` and `time_dependence`:
89+
For time and variable dependence:
8390

8491
```@repl traits
8592
struct MyObject end
@@ -91,24 +98,40 @@ Traits.has_variable_dependence_trait(::MyObject) = true
9198
Traits.variable_dependence(::MyObject) = Traits.Fixed
9299
93100
obj = MyObject()
94-
Traits.is_autonomous(obj) # false (derived from time_dependence)
95-
Traits.is_nonautonomous(obj) # true
96-
Traits.is_variable(obj) # false (derived from variable_dependence)
97-
Traits.is_nonvariable(obj) # true
101+
Traits.is_autonomous(obj)
102+
Traits.is_nonautonomous(obj)
103+
Traits.is_variable(obj)
104+
Traits.is_nonvariable(obj)
105+
```
106+
107+
For mutability, the same pattern applies with `has_mutability_trait` and `mutability`:
108+
109+
```@repl traits
110+
struct MyMutableObject end
111+
112+
Traits.has_mutability_trait(::MyMutableObject) = true
113+
Traits.mutability(::MyMutableObject) = Traits.InPlace
114+
115+
Traits.is_inplace(MyMutableObject())
116+
Traits.is_outofplace(MyMutableObject())
98117
```
99118

100119
If a type does not declare a trait, the predicates throw an informative error
101120
rather than returning a wrong default:
102121

103122
```@repl traits
123+
try # hide
104124
Traits.is_autonomous(3.14)
125+
catch e # hide
126+
showerror(IOContext(stdout, :color => false), e) # hide
127+
end # hide
105128
```
106129

107-
!!! note "Single source of truth"
108-
`Traits.Autonomous` / `Traits.NonAutonomous` are the very types used as the
109-
`CTModels.Model` time-dependence parameter, so `Traits.time_dependence(model)`
110-
and `Traits.is_autonomous(model)` work out of the box on a real optimal
111-
control problem.
130+
!!! note "Trait types are shared"
131+
Because trait types (e.g. `Traits.Autonomous`) are defined in a single place,
132+
a type that carries `Traits.Autonomous` as a type parameter and an object that
133+
implements `time_dependence` returning `Traits.Autonomous` are compatible by
134+
construction — no conversion or mapping needed.
112135

113136
## Accessor / predicate summary
114137

@@ -120,3 +143,7 @@ Traits.is_autonomous(3.14)
120143
| [`Traits.ad_trait`](@ref CTBase.Traits.ad_trait) ||
121144
| [`Traits.variable_costate_trait`](@ref CTBase.Traits.variable_costate_trait) ||
122145
| [`Traits.dynamics_trait`](@ref CTBase.Traits.dynamics_trait) ||
146+
147+
## See Also
148+
149+
- [Exceptions guide](exceptions.md) — understanding `IncorrectArgument` and `NotImplemented`.

docs/src/index.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ It provides the **base layer** shared by all packages: common types, structured
2727
| [`CTBase.Core`](@ref) | Fundamental numeric type alias (`ctNumber`) and internal display helpers |
2828
| [`CTBase.Descriptions`](@ref) | Symbolic description tuples: catalogue management, pattern completion, similarity search |
2929
| [`CTBase.Exceptions`](@ref) | Typed exception hierarchy with rich context fields |
30+
| [`CTBase.Traits`](@ref) | Compile-time trait types for time dependence, variable dependence, mutability, and dynamics dispatch |
3031
| [`CTBase.DevTools`](@ref) | Developer tools with tag-based dispatch for `run_tests`, `postprocess_coverage`, and `automatic_reference_documentation` |
3132
| [`CTBase.Unicode`](@ref) | Unicode subscript/superscript helpers for display |
3233

@@ -35,6 +36,7 @@ It provides the **base layer** shared by all packages: common types, structured
3536
- **[Getting Started](getting-started.md)** — installation, mental model, 5-minute walkthrough.
3637
- **[Descriptions](guide/descriptions.md)** — catalogue API, pattern matching, error handling.
3738
- **[Exceptions](guide/exceptions.md)** — exception hierarchy, choosing the right type, best practices.
39+
- **[Traits](guide/traits.md)** — compile-time trait types, the opt-in contract, and predicate functions.
3840
- **[Test Runner](guide/test-runner.md)** — modular test infrastructure with `CTBase.DevTools.run_tests`.
3941
- **[Coverage](guide/coverage.md)** — post-processing coverage artifacts with `CTBase.postprocess_coverage`.
4042
- **[API Documentation](guide/api-documentation.md)** — auto-generating per-module API pages.

src/Traits/Traits.jl

Lines changed: 41 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,47 @@
11
"""
22
Traits
33
4-
Trait types and trait-based dispatch shared across the control-toolbox ecosystem.
5-
6-
This module provides the trait system used for compile-time dispatch on:
7-
- Time dependence: [`Autonomous`](@ref), [`NonAutonomous`](@ref)
8-
- Variable dependence: [`Fixed`](@ref), [`NonFixed`](@ref)
9-
- Integration mode: [`EndPointMode`](@ref), [`TrajectoryMode`](@ref)
10-
- Dynamics type: [`StateDynamics`](@ref), [`HamiltonianDynamics`](@ref), [`AugmentedHamiltonianDynamics`](@ref)
11-
- Mutability: [`InPlace`](@ref), [`OutOfPlace`](@ref)
12-
- Automatic differentiation: [`WithAD`](@ref), [`WithoutAD`](@ref)
13-
- Variable costate capability: [`SupportsVariableCostate`](@ref), [`NoVariableCostate`](@ref)
14-
15-
Traits are used as type parameters in configuration types, vector fields, and systems
16-
to enable static dispatch without runtime type checks.
17-
18-
The time-dependence trait types (`TimeDependence`, `Autonomous`, `NonAutonomous`)
19-
historically lived in `CTModels.Components`; they are now defined here so the whole
20-
ecosystem shares a single set of types (CTModels consumes them from here).
4+
Compile-time trait types and trait-based dispatch utilities.
5+
6+
Traits are abstract or empty concrete types used as type parameters or returned
7+
by accessor functions. Behaviour is selected by dispatch at compile time, with
8+
no runtime cost.
9+
10+
# Organization
11+
12+
- **abstract.jl**: Root abstract type `AbstractTrait` and family abstractions
13+
- **time_dependence.jl**: Time-dependence traits and the opt-in contract
14+
- **variable_dependence.jl**: Variable-dependence traits and the opt-in contract
15+
- **mutability.jl**: Mutability traits and the opt-in contract
16+
- **mode.jl**: Integration-mode traits (`EndPointMode`, `TrajectoryMode`)
17+
- **dynamics.jl**: Dynamics-type traits (`StateDynamics`, `HamiltonianDynamics`, `AugmentedHamiltonianDynamics`)
18+
- **ad.jl**: Automatic-differentiation traits (`WithAD`, `WithoutAD`)
19+
- **variable_costate.jl**: Variable-costate traits (`SupportsVariableCostate`, `NoVariableCostate`)
20+
- **helpers.jl**: Internal utility (`_caller_function_name`)
21+
22+
# Public API
23+
24+
## Trait families
25+
26+
- **Time dependence**: `TimeDependence`, `Autonomous`, `NonAutonomous`
27+
- **Variable dependence**: `VariableDependence`, `Fixed`, `NonFixed`
28+
- **Mutability**: `InPlace`, `OutOfPlace`
29+
- **Integration mode**: `EndPointMode`, `TrajectoryMode`
30+
- **Dynamics**: `StateDynamics`, `HamiltonianDynamics`, `AugmentedHamiltonianDynamics`
31+
- **Automatic differentiation**: `WithAD`, `WithoutAD`
32+
- **Variable costate**: `SupportsVariableCostate`, `NoVariableCostate`
33+
34+
## Trait contract
35+
36+
For time-dependence, variable-dependence, and mutability, a type opts in by
37+
implementing two methods — `has_<family>_trait` returning `true`, and an accessor
38+
(`time_dependence`, `variable_dependence`, `mutability`) returning the trait type.
39+
Boolean predicates (`is_autonomous`, `is_variable`, `is_inplace`, …) are then
40+
derived generically.
41+
42+
The remaining families (`EndPointMode`, `StateDynamics`, `WithAD`,
43+
`SupportsVariableCostate`) are used as type parameters only and do not use the
44+
two-method contract.
2145
"""
2246
module Traits
2347

src/Traits/abstract.jl

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,20 @@
11
"""
22
$(TYPEDEF)
33
4-
Abstract base type for trait markers in CTFlows.
4+
Abstract base type for all trait markers.
55
6-
Traits are empty marker types used as type parameters to encode configuration
7-
properties at compile time. Unlike tags (which mark extension implementations),
8-
traits encode semantic properties of the configuration itself (e.g., integration
9-
mode, content type, mutability).
10-
11-
# Trait Pattern
12-
13-
Traits are used as type parameters in abstract configuration types to enable
14-
compile-time dispatch without runtime type checks. For example, `AbstractConfig`
15-
uses `EndPointMode` vs `TrajectoryMode` to distinguish integration modes, and
16-
`StateDynamics` vs `HamiltonianDynamics` to distinguish dynamics types.
17-
18-
All concrete trait types are empty structs with no fields, making them zero-cost
19-
at runtime.
6+
Traits are empty marker types used as type parameters to encode properties at
7+
compile time. Unlike tags (which mark extension implementations), traits encode
8+
semantic properties of an object (e.g., integration mode, dynamics type,
9+
mutability). All concrete trait types are empty structs with no fields, making
10+
them zero-cost at runtime.
2011
2112
# Interface Requirements
2213
2314
Concrete trait subtypes should:
2415
- Be empty structs with no fields (pure markers)
2516
- Subtype an intermediate abstract trait category (e.g., `AbstractModeTrait`)
26-
- Be used as type parameters in configuration types
17+
- Be used as type parameters or returned by accessor functions
2718
2819
# Example
2920
\`\`\`julia-repl
@@ -34,10 +25,6 @@ true
3425
3526
julia> EndPointMode <: Traits.AbstractModeTrait
3627
true
37-
38-
julia> # Used as type parameters in configs:
39-
julia> StateEndPointConfig <: CTFlows.Configs.AbstractConfig{<:Any, EndPointMode, StateDynamics}
40-
true
4128
\`\`\`
4229
4330
# Notes

src/Traits/ad.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ true
3131
- `WithoutAD` indicates the system uses pre-computed derivatives or manual implementations
3232
- The specific meaning depends on the system type and context
3333
34-
See also: [`CTBase.Traits.WithAD`](@ref), [`CTBase.Traits.WithoutAD`](@ref), [`CTBase.Core.AbstractCache`](@ref).
34+
See also: [`CTBase.Traits.WithAD`](@ref), [`CTBase.Traits.WithoutAD`](@ref), `CTBase.Core.AbstractCache`.
3535
"""
3636
abstract type AbstractADTrait <: AbstractTrait end
3737

@@ -66,7 +66,7 @@ true
6666
- The cache is passed through parameters during integration or evaluation
6767
- The specific operations enabled depend on the system type and AD backend
6868
69-
See also: [`CTBase.Traits.AbstractADTrait`](@ref), [`CTBase.Traits.WithoutAD`](@ref), [`CTBase.Core.AbstractCache`](@ref).
69+
See also: [`CTBase.Traits.AbstractADTrait`](@ref), [`CTBase.Traits.WithoutAD`](@ref), `CTBase.Core.AbstractCache`.
7070
"""
7171
struct WithAD <: AbstractADTrait end # system carries H + AD backend
7272

src/Traits/dynamics.jl

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,13 @@ true
1616
1717
julia> HamiltonianDynamics <: Traits.AbstractDynamicsTrait
1818
true
19-
20-
julia> # Used in configuration type parameters:
21-
julia> StateEndPointConfig <: CTFlows.Configs.AbstractConfig{<:Any, <:Traits.AbstractModeTrait, StateDynamics}
22-
true
2319
\`\`\`
2420
2521
# Notes
26-
- Dynamics traits are used as the third type parameter in `AbstractConfigWithMaC`
2722
- State dynamics indicates configurations with only state variables (no costate)
2823
- Hamiltonian dynamics indicates configurations with both state and costate variables
2924
30-
See also: [`CTBase.Traits.StateDynamics`](@ref), [`CTBase.Traits.HamiltonianDynamics`](@ref), [`CTFlows.Configs.AbstractConfig`](@ref).
25+
See also: [`CTBase.Traits.StateDynamics`](@ref), [`CTBase.Traits.HamiltonianDynamics`](@ref).
3126
"""
3227
abstract type AbstractDynamicsTrait <: AbstractTrait end
3328

@@ -49,17 +44,14 @@ StateDynamics()
4944
julia> st isa Traits.AbstractDynamicsTrait
5045
true
5146
52-
julia> # Used in state-only configurations:
53-
julia> StateEndPointConfig <: CTFlows.Configs.AbstractConfig{<:Any, <:Traits.AbstractModeTrait, StateDynamics}
54-
true
5547
\`\`\`
5648
5749
# Notes
5850
- State configurations store only `x0` (initial state)
5951
- The `initial_costate` accessor throws a `PreconditionError` for state configurations
6052
- This mode is suitable for standard ODE integration without adjoint variables
6153
62-
See also: [`CTBase.Traits.HamiltonianDynamics`](@ref), [`CTBase.Traits.AbstractDynamicsTrait`](@ref), [`CTFlows.Configs.StateEndPointConfig`](@ref).
54+
See also: [`CTBase.Traits.HamiltonianDynamics`](@ref), [`CTBase.Traits.AbstractDynamicsTrait`](@ref).
6355
"""
6456
struct StateDynamics <: AbstractDynamicsTrait end
6557

@@ -81,17 +73,14 @@ HamiltonianDynamics()
8173
julia> ham isa Traits.AbstractDynamicsTrait
8274
true
8375
84-
julia> # Used in Hamiltonian configurations:
85-
julia> HamiltonianEndPointConfig <: CTFlows.Configs.AbstractConfig{<:Any, <:Traits.AbstractModeTrait, HamiltonianDynamics}
86-
true
8776
\`\`\`
8877
8978
# Notes
9079
- Hamiltonian configurations store both `x0` (initial state) and `p0` (initial costate)
9180
- The `initial_condition` accessor returns `vcat(x0, p0)` for Hamiltonian configurations
9281
- This mode is suitable for optimal control problems with Pontryagin's maximum principle
9382
94-
See also: [`CTBase.Traits.StateDynamics`](@ref), [`CTBase.Traits.AbstractDynamicsTrait`](@ref), [`CTFlows.Configs.HamiltonianEndPointConfig`](@ref).
83+
See also: [`CTBase.Traits.StateDynamics`](@ref), [`CTBase.Traits.AbstractDynamicsTrait`](@ref).
9584
"""
9685
struct HamiltonianDynamics <: AbstractDynamicsTrait end
9786

@@ -101,11 +90,10 @@ $(TYPEDEF)
10190
Trait marker for augmented Hamiltonian dynamics, where the Hamiltonian includes an augmented variable (e.g., a parameter or control variable) in addition to state and costate variables.
10291
10392
# Notes
104-
- Used in conjunction with [`CTFlows.Configs.AbstractAugmentedHamiltonianConfig`](@ref) to specify that a configuration is for an augmented Hamiltonian system.
10593
- Subtypes [`CTBase.Traits.AbstractDynamicsTrait`](@ref).
10694
- Used to distinguish augmented Hamiltonian systems from standard Hamiltonian systems in trait-based dispatch.
10795
108-
See also: [`CTBase.Traits.HamiltonianDynamics`](@ref), [`CTFlows.Configs.AbstractAugmentedHamiltonianConfig`](@ref), [`CTBase.Traits.AbstractDynamicsTrait`](@ref).
96+
See also: [`CTBase.Traits.HamiltonianDynamics`](@ref), [`CTBase.Traits.AbstractDynamicsTrait`](@ref).
10997
"""
11098
struct AugmentedHamiltonianDynamics <: AbstractDynamicsTrait end
11199

0 commit comments

Comments
 (0)