From 09a3b280a160b8f0d2749b143f18dc4b1822abf3 Mon Sep 17 00:00:00 2001 From: Bronek Kozicki Date: Thu, 24 Sep 2026 20:40:48 +0100 Subject: [PATCH 1/2] Add tuple protocol support for singular copacks Provide tuple_size, tuple_element and ADL get<0> for copack. Structured bindings now expose the alternative rather than the public storage members. Empty and wider copacks remain excluded. Preserve const qualification and value category in get, and reject volatile copacks through constraints. A pack alternative remains one tuple element; fn::apply still expands its fields. Cover tuple traits, reference access, structured bindings, choice payload mutation and apply dispatch. Assisted-by: Claude:claude-opus-5-5 --- CHANGELOG.md | 10 +++ TYPE_ALGEBRA.md | 2 +- docs/reference/pack.md | 13 ++- include/fn/copack.hpp | 35 +++++++- tests/fn/copack.cpp | 183 +++++++++++++++++++++++++++++++++------ tests/fn/functional.cpp | 13 +++ tests/fn/just_choice.cpp | 16 ++++ 7 files changed, 238 insertions(+), 34 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 20237d04..5d276778 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,16 @@ Design history of libfn, newest first. The living documents — [README.md](README.md), [CONTRIBUTING.md](CONTRIBUTING.md), [docs/](docs/) — describe only the present state of the design; when a decision makes an earlier idea obsolete, this file is where the transition is recorded and explained. +## Singular copacks support the tuple protocol — 24 September 2026 + +A `copack` with exactly one alternative supports the tuple protocol: `std::tuple_size_v>` is 1, `std::tuple_element_t<0, copack>` is `T`, and `get<0>` returns the same reference as the index-less `get`. This supports structured bindings and generic code that uses tuple traits with ADL `get`. A structured binding over a singular copack, including a singular choice's `value()`, now binds its alternative. Previously, it bound the public `data` and `index` members. + +The protocol is limited to singular copacks. A wider copack selects among different alternative types at run time, so there is no single element type to expose. `copack<>` remains outside the protocol: it has no values, whereas the empty product `pack<>` represents the unit. + +For `copack>`, the tuple element is the entire `pack`. `fn::apply` still dispatches the copack and passes the pack's fields as separate arguments; `pfn::apply` does not accept copacks. + +Both `get` overloads exclude volatile copacks through their constraints. Previously, probing the index-less `get` with a volatile copack could produce an error in its body. + ## `choice` becomes `just` over a copack — 21 September 2026 `choice` is now an alias of `just>`. The specialization forwards operations on alternatives to its `copack` payload. The `` header is gone; `` declares `choice`, `choice_for` and `some_choice`. diff --git a/TYPE_ALGEBRA.md b/TYPE_ALGEBRA.md index 766121e9..5047a764 100644 --- a/TYPE_ALGEBRA.md +++ b/TYPE_ALGEBRA.md @@ -342,7 +342,7 @@ As a payload, a `copack` models a discriminated union of types. Evaluating a `copack` via `.apply()` passes the active alternative to the callback. Because `copack` is self-flattening, nested `copack`s do not occur. A selected alternative that is itself tuple-like—such as `pack`, `std::tuple`, or `std::array`—is unpacked one level, passing its immediate constituents as separate arguments. Since normalized shapes are sums of products, one level of unpacking is sufficient to supply the product's fields as function arguments. -To explicitly lift a single scalar value into a single-alternative coproduct, use `fn::as_copack(value)`. Unlike `as_pack`, it always decays: a `copack` alternative can never be a reference. When a `copack` contains exactly one alternative, it is **singular** and supports direct value extraction via the `get` utility (resolvable via ADL), which propagates references with the same semantics as `apply`. +To explicitly lift a single scalar value into a single-alternative coproduct, use `fn::as_copack(value)`. Unlike `as_pack`, it always decays: a `copack` alternative can never be a reference. When a `copack` contains exactly one alternative, it is **singular** and supports structured bindings through `std::tuple_size`, `std::tuple_element` and ADL `get<0>`. The index-less `get` returns the same reference, preserving const qualification and value category; neither overload accepts a volatile copack. The tuple element is the entire alternative: `copack>` exposes one `pack` element, while `apply` passes the pack's two fields as separate arguments. A `pack` can be lifted into a `copack` (including packs holding references), but a `pack` cannot contain a `copack`. There is algebraic equivalence between a hypothetical `pack` containing a `copack` (which is disallowed) and a specific shape of `copack` containing a `pack` — see Section 6 for details. diff --git a/docs/reference/pack.md b/docs/reference/pack.md index 936610e1..f898672d 100644 --- a/docs/reference/pack.md +++ b/docs/reference/pack.md @@ -123,17 +123,26 @@ constexpr auto as_pack(std::type_identity_t src, std::type_identity_t.. ## get {style: "api"} ```cpp {title: "fn::get"} -template +template constexpr auto get(Cp &&c) -> decltype(auto); // (1) +template +constexpr auto get(C &&c) -> decltype(auto); // (2) + template -constexpr auto get(P &&p) -> decltype(auto); // (2) +constexpr auto get(P &&p) -> decltype(auto); // (3) ``` :include-doxygen-doc: fn::get { args: "Cp &&" } :include-doxygen-doc-params: fn::get { args: "Cp &&", title: "parameters" } +:include-doxygen-doc: fn::get { args: "C &&" } + +:include-doxygen-doc-params: fn::get { args: "C &&", type: "template", title: "template parameters" } + +:include-doxygen-doc-params: fn::get { args: "C &&", title: "parameters" } + :include-doxygen-doc: fn::get { args: "P &&" } :include-doxygen-doc-params: fn::get { args: "P &&", type: "template", title: "template parameters" } diff --git a/include/fn/copack.hpp b/include/fn/copack.hpp index 5fa4dc4e..d1b5868e 100644 --- a/include/fn/copack.hpp +++ b/include/fn/copack.hpp @@ -1247,13 +1247,13 @@ template struct _sole_alternative<::fn::copack> { * @brief Accesses the sole alternative of a singular copack * * Only a `copack` with exactly one alternative qualifies - the access needs no dispatch and cannot - * miss. Returns the alternative carrying the copack's cv-qualification and value category, exactly - * as `apply` would pass it. + * miss. Returns the entire alternative, preserving const qualification and value category. + * Volatile copacks are excluded. * * @param c The singular copack, in any value category * @return Reference to the sole alternative */ -template +template requires requires { typename detail::_sole_alternative<::std::remove_cvref_t>::type; } [[nodiscard]] constexpr decltype(auto) get(Cp &&c) noexcept { @@ -1264,6 +1264,25 @@ template return ::std::move(*c.get_ptr(::std::in_place_type)); } +/** + * @brief Tuple-protocol element access to the sole alternative of a singular copack + * + * `std::tuple_size_v>` is 1 and `std::tuple_element_t<0, copack>` is `T`. + * `get<0>` returns the same reference as the index-less `get`, and excludes volatile copacks. + * ADL makes it available to structured bindings and generic code using `using std::get; get<0>(c);`. + * A `pack` alternative is returned whole; `apply` passes its fields as separate arguments. + * + * @tparam I Element index; only 0 + * @param c The singular copack, in any value category + * @return Reference to the sole alternative + */ +template <::std::size_t I, some_copack C> + requires(I == 0) && requires { typename detail::_sole_alternative<::std::remove_cvref_t>::type; } +[[nodiscard]] constexpr decltype(auto) get(C &&c) noexcept +{ + return ::fn::get(FWD(c)); +} + namespace detail { // The graded joins for expected's binds over a copack side, when the exhaustive branches return // DIFFERENT expected types. Sets convergent in the exact result type fall back to the select @@ -1524,6 +1543,16 @@ template } // namespace LIBFN_VERSION } // namespace fn +// Wider copacks have no single alternative type to expose as a tuple element. +// copack<> has no values; treating it as an empty tuple would obscure its distinction from pack<>. +namespace std { +template struct tuple_size<::fn::copack> : ::std::integral_constant<::std::size_t, 1> {}; + +template struct tuple_element<0, ::fn::copack> { + using type = T; +}; +} // namespace std + #include #endif // INCLUDE_FN_COPACK diff --git a/tests/fn/copack.cpp b/tests/fn/copack.cpp index bcc492ff..749b44f3 100644 --- a/tests/fn/copack.cpp +++ b/tests/fn/copack.cpp @@ -123,6 +123,15 @@ concept can_apply_type = requires(S s, Fn fn, Args... args) { FWD(s).apply_type( template concept can_apply_type_r = requires(S s, Fn fn, Args... args) { FWD(s).template apply_type_r(FWD(fn), FWD(args)...); }; + +template +concept can_get = requires(S s) { fn::get(FWD(s)); }; + +template +concept can_get_at = requires(S s) { fn::get(FWD(s)); }; + +template +concept has_tuple_size = requires { std::tuple_size::value; }; } // anonymous namespace // A copack brace-initializes the alternative it stores. That is a DESIGN DIRECTION, not an @@ -2877,46 +2886,164 @@ TEST_CASE("copack emplace", "[copack][emplace]") } } -TEST_CASE("get on a singular copack", "[copack][get]") +TEST_CASE("copack get and tuple protocol", "[copack][get][tuple]") { struct A final { int v; constexpr bool operator==(A const &) const = default; }; - constexpr auto can_get = [](auto &&c) { return requires { fn::get(FWD(c)); }; }; - - // the alternative comes back carrying the copack's cv-qualification and value category, exactly - // as apply would pass it - fn::copack c{A{7}}; - static_assert(std::is_same_v); - static_assert(std::is_same_v); - static_assert(std::is_same_v); - static_assert(std::is_same_v); - static_assert(noexcept(fn::get(c))); - CHECK(fn::get(c) == A{7}); - fn::get(c).v = 9; - CHECK(fn::get(std::as_const(c)) == A{9}); - CHECK(fn::get(std::move(c)) == A{9}); + using C = fn::copack; + + SECTION("get") + { + // Access preserves const qualification and all four value categories. + C c{A{7}}; + static_assert(std::is_same_v); + static_assert(std::is_same_v); + static_assert(std::is_same_v); + static_assert(std::is_same_v); + static_assert(noexcept(fn::get(c))); + CHECK(fn::get(c) == A{7}); + fn::get(c).v = 9; + CHECK(fn::get(std::as_const(c)) == A{9}); + CHECK(fn::get(std::move(c)) == A{9}); - SECTION("constexpr") + SECTION("constexpr") + { + constexpr C cc{A{5}}; + static_assert(fn::get(cc) == A{5}); + static_assert([] { + C m{A{1}}; + fn::get(m).v = 2; + return fn::get(std::move(m)).v; + }() == 2); + + SUCCEED(); + } + + SECTION("constraints") + { + // Direct access requires exactly one alternative. + static_assert(can_get); + static_assert(not can_get>); + static_assert(not can_get); + // Volatile access is excluded by the constraints. + static_assert(can_get); + static_assert(not can_get); + + SUCCEED(); + } + } + + SECTION("get<0>") { - constexpr fn::copack cc{A{5}}; - static_assert(fn::get(cc) == A{5}); - static_assert([] { - fn::copack m{A{1}}; - fn::get(m).v = 2; - return fn::get(std::move(m)).v; - }() == 2); + // Indexed access returns the same reference as index-less access. + C c{A{7}}; + static_assert(std::is_same_v(c)), A &>); + static_assert(std::is_same_v(std::as_const(c))), A const &>); + static_assert(std::is_same_v(std::move(c))), A &&>); + static_assert(std::is_same_v(std::move(std::as_const(c)))), A const &&>); + static_assert(noexcept(fn::get<0>(c))); + CHECK(&fn::get<0>(c) == &fn::get(c)); + fn::get<0>(c).v = 9; + CHECK(fn::get(std::as_const(c)) == A{9}); + { + // unqualified get with std::get also in scope resolves to fn::get by ADL + using std::get; + CHECK(get<0>(std::move(c)) == A{9}); + } + + SECTION("constexpr") + { + static_assert([] { + C m{A{1}}; + fn::get<0>(m).v = 2; + return &fn::get<0>(m) == &fn::get(m) && fn::get<0>(std::move(m)).v == 2; + }()); + + SUCCEED(); + } + + SECTION("constraints") + { + static_assert(can_get_at); + static_assert(not can_get_at); + static_assert(not can_get_at &, 0>); + static_assert(not can_get_at); + static_assert(not can_get_at); + + SUCCEED(); + } + } + + SECTION("tuple_size and tuple_element") + { + static_assert(std::tuple_size_v == 1); + static_assert(std::tuple_size_v == 1); + static_assert(std::is_same_v, A>); + static_assert(std::is_same_v, A const>); + // The tuple protocol excludes both empty and multiple-alternative copacks. + static_assert(has_tuple_size>); + static_assert(not has_tuple_size>); + static_assert(not has_tuple_size>); SUCCEED(); } - SECTION("constraints") + SECTION("structured bindings") + { + C c{A{7}}; + auto &[a] = c; + static_assert(std::is_same_v); + a.v = 8; + CHECK(fn::get(c) == A{8}); + auto const &[ca] = c; + static_assert(std::is_same_v); + CHECK(&ca == &a); + auto &&[r] = std::move(c); + static_assert(std::is_same_v); + CHECK(&r == &a); + auto &&[cr] = std::move(std::as_const(c)); + static_assert(std::is_same_v); + CHECK(&cr == &a); + auto [m] = std::move(c); + static_assert(std::is_same_v); + CHECK(m == A{8}); + CHECK(&m != &a); + + static_assert([] { + C q{A{5}}; + auto &[e] = q; + e.v = 6; + auto const &[ce] = q; + auto &&[re] = std::move(q); + auto &&[cre] = std::move(std::as_const(q)); + auto [v] = std::move(q); + return fn::get(q).v == 6 && &ce == &e && &re == &e && &cre == &e && v == A{6} && &v != &e; + }()); + } + + SECTION("one-element pack") { - // only the singular copack qualifies; a multi-alternative one dispatches, it does not get - static_assert(can_get(fn::copack{A{1}})); - static_assert(not can_get(fn::copack_for{1})); - static_assert(not can_get(A{1})); + // These tuple traits and access types match those of pack. + using P = fn::pack; + static_assert(std::tuple_size_v == std::tuple_size_v

); + static_assert(std::is_same_v, std::tuple_element_t<0, P>>); + static_assert(std::is_same_v, std::tuple_element_t<0, P const>>); + static_assert(std::is_same_v(std::declval())), decltype(fn::get<0>(std::declval

()))>); + static_assert(std::is_same_v(std::declval())), + decltype(fn::get<0>(std::declval

()))>); + static_assert( + std::is_same_v(std::declval())), decltype(fn::get<0>(std::declval

()))>); + static_assert(std::is_same_v(std::declval())), + decltype(fn::get<0>(std::declval

()))>); + + // A pack alternative is one tuple element; apply passes its two fields separately. + using CP = fn::copack>; + constexpr auto arity = [](auto &&...args) noexcept { return (0 + ... + (static_cast(args), 1)); }; + static_assert(std::tuple_size_v == 1); + static_assert(std::is_same_v, fn::pack>); + static_assert(CP{fn::pack{1, 2}}.apply(arity) == 2); SUCCEED(); } diff --git a/tests/fn/functional.cpp b/tests/fn/functional.cpp index 37d2b091..cab51ee2 100644 --- a/tests/fn/functional.cpp +++ b/tests/fn/functional.cpp @@ -308,6 +308,19 @@ TEST_CASE("apply tuple-like", "[apply][apply_r][tuple]") CHECK(fn::apply(arity, fn::pack{1, 2, 3}) == 3); } + SECTION("singular copack dispatch with a pack alternative") + { + // fn::apply passes the pack's two fields, although the tuple protocol exposes one element. + // pfn::apply rejects this copack and accepts a std::tuple containing the same pack type. + using C = fn::copack>; + static_assert(std::tuple_size_v == 1); + static_assert(fn::apply(arity, C{fn::pack{1, 2}}) == 2); + static_assert(not pfn::is_applicable_v); + static_assert(pfn::is_applicable_v>>); + + CHECK(fn::apply(arity, C{fn::pack{1, 2}}) == 2); + } + SECTION("apply_r over the elements") { static_assert(fn::apply_r(add2, std::tuple{2, 3}) == 5L); diff --git a/tests/fn/just_choice.cpp b/tests/fn/just_choice.cpp index 74943fde..91497e3d 100644 --- a/tests/fn/just_choice.cpp +++ b/tests/fn/just_choice.cpp @@ -140,6 +140,22 @@ TEST_CASE("choice non-monadic functionality", "[choice]") CHECK((std::as_const(s).value().apply(fn)) == 44); CHECK((std::move(std::as_const(s)).value().apply(fn)) == 46); CHECK((std::move(s).value().apply(fn)) == 45); + + SECTION("singular") + { + // A reference binding to the sole payload alternative can modify the choice. + static_assert(std::tuple_size_v::value_type> == std::tuple_size_v>); + auto c = fn::as_choice(12); + auto &[i] = c.value(); + i = 42; + CHECK(c == choice{42}); + static_assert([] { + auto d = fn::as_choice(12); + auto &[j] = d.value(); + j = 42; + return d == choice{42}; + }()); + } } SECTION("choice_for") From eb307875ce11de612ccd9d6f3baa50efc36aa3be Mon Sep 17 00:00:00 2001 From: Bronek Kozicki Date: Fri, 25 Sep 2026 20:35:33 +0100 Subject: [PATCH 2/2] Cut down documentation of singular copack --- TYPE_ALGEBRA.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/TYPE_ALGEBRA.md b/TYPE_ALGEBRA.md index 5047a764..eca36afa 100644 --- a/TYPE_ALGEBRA.md +++ b/TYPE_ALGEBRA.md @@ -342,7 +342,7 @@ As a payload, a `copack` models a discriminated union of types. Evaluating a `copack` via `.apply()` passes the active alternative to the callback. Because `copack` is self-flattening, nested `copack`s do not occur. A selected alternative that is itself tuple-like—such as `pack`, `std::tuple`, or `std::array`—is unpacked one level, passing its immediate constituents as separate arguments. Since normalized shapes are sums of products, one level of unpacking is sufficient to supply the product's fields as function arguments. -To explicitly lift a single scalar value into a single-alternative coproduct, use `fn::as_copack(value)`. Unlike `as_pack`, it always decays: a `copack` alternative can never be a reference. When a `copack` contains exactly one alternative, it is **singular** and supports structured bindings through `std::tuple_size`, `std::tuple_element` and ADL `get<0>`. The index-less `get` returns the same reference, preserving const qualification and value category; neither overload accepts a volatile copack. The tuple element is the entire alternative: `copack>` exposes one `pack` element, while `apply` passes the pack's two fields as separate arguments. +To explicitly lift a scalar value into a single-alternative coproduct (i.e. *singular* `copack`), use `fn::as_copack(value)`. Unlike `as_pack`, it always decays: a `copack` alternative can never be a reference. A singular `copack` supports the standard C++ tuple protocol (`get<0>`, `tuple_size` returning 1, `tuple_element<0, copack<...>>`, structured bindings) and additionally provides index-less `get`. A `pack` can be lifted into a `copack` (including packs holding references), but a `pack` cannot contain a `copack`. There is algebraic equivalence between a hypothetical `pack` containing a `copack` (which is disallowed) and a specific shape of `copack` containing a `pack` — see Section 6 for details.