Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<copack<T>>` is 1, `std::tuple_element_t<0, copack<T>>` 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<pack<A, B>>`, 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<Ts...>` is now an alias of `just<copack<Ts...>>`. The specialization forwards operations on alternatives to its `copack` payload. The `<fn/choice.hpp>` header is gone; `<fn/just.hpp>` declares `choice`, `choice_for` and `some_choice`.
Expand Down
2 changes: 1 addition & 1 deletion TYPE_ALGEBRA.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 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.

Expand Down
13 changes: 11 additions & 2 deletions docs/reference/pack.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,17 +123,26 @@ constexpr auto as_pack(std::type_identity_t<T> src, std::type_identity_t<Args>..
## get {style: "api"}

```cpp {title: "fn::get"}
template <typename Cp>
template <some_copack Cp>
constexpr auto get(Cp &&c) -> decltype(auto); // (1)

template <std::size_t I, some_copack C>
constexpr auto get(C &&c) -> decltype(auto); // (2)

template <std::size_t I, some_pack P>
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" }
Expand Down
35 changes: 32 additions & 3 deletions include/fn/copack.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1247,13 +1247,13 @@ template <typename T> struct _sole_alternative<::fn::copack<T>> {
* @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 <typename Cp>
template <some_copack Cp>
requires requires { typename detail::_sole_alternative<::std::remove_cvref_t<Cp>>::type; }
[[nodiscard]] constexpr decltype(auto) get(Cp &&c) noexcept
{
Expand All @@ -1264,6 +1264,25 @@ template <typename Cp>
return ::std::move(*c.get_ptr(::std::in_place_type<type>));
}

/**
* @brief Tuple-protocol element access to the sole alternative of a singular copack
*
* `std::tuple_size_v<copack<T>>` is 1 and `std::tuple_element_t<0, copack<T>>` 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<C>>::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
Expand Down Expand Up @@ -1524,6 +1543,16 @@ template <typename Tag, typename Cp, typename Fn>
} // 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 <typename T> struct tuple_size<::fn::copack<T>> : ::std::integral_constant<::std::size_t, 1> {};

template <typename T> struct tuple_element<0, ::fn::copack<T>> {
using type = T;
};
} // namespace std

#include <fn/detail/macro_end.hpp>

#endif // INCLUDE_FN_COPACK
183 changes: 155 additions & 28 deletions tests/fn/copack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,15 @@ concept can_apply_type = requires(S s, Fn fn, Args... args) { FWD(s).apply_type(
template <typename S, typename R, typename Fn, typename... Args>
concept can_apply_type_r
= requires(S s, Fn fn, Args... args) { FWD(s).template apply_type_r<R>(FWD(fn), FWD(args)...); };

template <typename S>
concept can_get = requires(S s) { fn::get(FWD(s)); };

template <typename S, std::size_t I>
concept can_get_at = requires(S s) { fn::get<I>(FWD(s)); };

template <typename T>
concept has_tuple_size = requires { std::tuple_size<T>::value; };
} // anonymous namespace

// A copack brace-initializes the alternative it stores. That is a DESIGN DIRECTION, not an
Expand Down Expand Up @@ -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<A> c{A{7}};
static_assert(std::is_same_v<decltype(fn::get(c)), A &>);
static_assert(std::is_same_v<decltype(fn::get(std::as_const(c))), A const &>);
static_assert(std::is_same_v<decltype(fn::get(std::move(c))), A &&>);
static_assert(std::is_same_v<decltype(fn::get(std::move(std::as_const(c)))), A const &&>);
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<A>;

SECTION("get")
{
// Access preserves const qualification and all four value categories.
C c{A{7}};
static_assert(std::is_same_v<decltype(fn::get(c)), A &>);
static_assert(std::is_same_v<decltype(fn::get(std::as_const(c))), A const &>);
static_assert(std::is_same_v<decltype(fn::get(std::move(c))), A &&>);
static_assert(std::is_same_v<decltype(fn::get(std::move(std::as_const(c)))), A const &&>);
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<C>);
static_assert(not can_get<fn::copack_for<A, int>>);
static_assert(not can_get<A>);
// Volatile access is excluded by the constraints.
static_assert(can_get<C &>);
static_assert(not can_get<C volatile &>);

SUCCEED();
}
}

SECTION("get<0>")
{
constexpr fn::copack<A> cc{A{5}};
static_assert(fn::get(cc) == A{5});
static_assert([] {
fn::copack<A> 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<decltype(fn::get<0>(c)), A &>);
static_assert(std::is_same_v<decltype(fn::get<0>(std::as_const(c))), A const &>);
static_assert(std::is_same_v<decltype(fn::get<0>(std::move(c))), A &&>);
static_assert(std::is_same_v<decltype(fn::get<0>(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<C &, 0>);
static_assert(not can_get_at<C &, 1>);
static_assert(not can_get_at<fn::copack_for<A, int> &, 0>);
static_assert(not can_get_at<C volatile &, 0>);
static_assert(not can_get_at<A &, 0>);

SUCCEED();
}
}

SECTION("tuple_size and tuple_element")
{
static_assert(std::tuple_size_v<C> == 1);
static_assert(std::tuple_size_v<C const> == 1);
static_assert(std::is_same_v<std::tuple_element_t<0, C>, A>);
static_assert(std::is_same_v<std::tuple_element_t<0, C const>, A const>);
// The tuple protocol excludes both empty and multiple-alternative copacks.
static_assert(has_tuple_size<fn::pack<>>);
static_assert(not has_tuple_size<fn::copack<>>);
static_assert(not has_tuple_size<fn::copack_for<A, int>>);

SUCCEED();
}

SECTION("constraints")
SECTION("structured bindings")
{
C c{A{7}};
auto &[a] = c;
static_assert(std::is_same_v<decltype(a), A>);
a.v = 8;
CHECK(fn::get(c) == A{8});
auto const &[ca] = c;
static_assert(std::is_same_v<decltype(ca), A const>);
CHECK(&ca == &a);
auto &&[r] = std::move(c);
static_assert(std::is_same_v<decltype(r), A>);
CHECK(&r == &a);
auto &&[cr] = std::move(std::as_const(c));
static_assert(std::is_same_v<decltype(cr), A const>);
CHECK(&cr == &a);
auto [m] = std::move(c);
static_assert(std::is_same_v<decltype(m), A>);
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>{A{1}}));
static_assert(not can_get(fn::copack_for<A, int>{1}));
static_assert(not can_get(A{1}));
// These tuple traits and access types match those of pack<A>.
using P = fn::pack<A>;
static_assert(std::tuple_size_v<C> == std::tuple_size_v<P>);
static_assert(std::is_same_v<std::tuple_element_t<0, C>, std::tuple_element_t<0, P>>);
static_assert(std::is_same_v<std::tuple_element_t<0, C const>, std::tuple_element_t<0, P const>>);
static_assert(std::is_same_v<decltype(fn::get<0>(std::declval<C &>())), decltype(fn::get<0>(std::declval<P &>()))>);
static_assert(std::is_same_v<decltype(fn::get<0>(std::declval<C const &>())),
decltype(fn::get<0>(std::declval<P const &>()))>);
static_assert(
std::is_same_v<decltype(fn::get<0>(std::declval<C &&>())), decltype(fn::get<0>(std::declval<P &&>()))>);
static_assert(std::is_same_v<decltype(fn::get<0>(std::declval<C const &&>())),
decltype(fn::get<0>(std::declval<P const &&>()))>);

// A pack alternative is one tuple element; apply passes its two fields separately.
using CP = fn::copack<fn::pack<int, int>>;
constexpr auto arity = [](auto &&...args) noexcept { return (0 + ... + (static_cast<void>(args), 1)); };
static_assert(std::tuple_size_v<CP> == 1);
static_assert(std::is_same_v<std::tuple_element_t<0, CP>, fn::pack<int, int>>);
static_assert(CP{fn::pack{1, 2}}.apply(arity) == 2);

SUCCEED();
}
Expand Down
13 changes: 13 additions & 0 deletions tests/fn/functional.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<fn::pack<int, int>>;
static_assert(std::tuple_size_v<C> == 1);
static_assert(fn::apply(arity, C{fn::pack{1, 2}}) == 2);
static_assert(not pfn::is_applicable_v<decltype(arity), C>);
static_assert(pfn::is_applicable_v<decltype(arity), std::tuple<fn::pack<int, int>>>);

CHECK(fn::apply(arity, C{fn::pack{1, 2}}) == 2);
}

SECTION("apply_r over the elements")
{
static_assert(fn::apply_r<long>(add2, std::tuple{2, 3}) == 5L);
Expand Down
16 changes: 16 additions & 0 deletions tests/fn/just_choice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<choice<int>::value_type> == std::tuple_size_v<fn::pack<int>>);
auto c = fn::as_choice(12);
auto &[i] = c.value();
i = 42;
CHECK(c == choice<int>{42});
static_assert([] {
auto d = fn::as_choice(12);
auto &[j] = d.value();
j = 42;
return d == choice<int>{42};
}());
}
}

SECTION("choice_for")
Expand Down
Loading