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
3 changes: 2 additions & 1 deletion include/functional/choice.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,8 @@ template <typename... Ts, typename... Tx>
return not(lh == rh);
}

template <typename... Ts> using choice_for = detail::normalized<Ts...>::template apply<choice>;
template <typename... Ts>
using choice_for = detail::_collapsing_sum::normalized<::fn::choice, detail::_collapsing_sum::flattened<Ts...>>::type;

} // namespace fn

Expand Down
173 changes: 105 additions & 68 deletions include/functional/expected.hpp

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions include/functional/optional.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ concept some_optional = detail::_some_optional<T>;

template <typename T> struct optional final : std::optional<T> {
using value_type = std::optional<T>::value_type;
static_assert(not std::is_same_v<value_type, ::fn::sum<>>);

using std::optional<T>::optional;

Expand Down
27 changes: 20 additions & 7 deletions include/functional/sum.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,17 @@ template <typename Fn, typename Self> struct _sum_invoke_result<_collapsing_sum_
} // namespace detail

template <typename... Ts> struct sum;
template <> struct sum<>; // Intentionally incomplete

// A unit of sum<> monoid, cannot be created but offers useful static functions and can be put in an union
template <> struct sum<> final {
constexpr sum() noexcept = delete; // NOTE, `= delete` here is the whole point
constexpr ~sum() noexcept = default;
constexpr sum(sum const &) noexcept = default;
constexpr sum(sum &&) noexcept = default;

static constexpr std::size_t size = 0;
template <typename T> static constexpr bool has_type = false;
};

template <typename... Ts>
requires(sizeof...(Ts) > 0)
Expand Down Expand Up @@ -143,8 +153,8 @@ struct sum<Ts...> {

template <typename... Tx>
constexpr sum(sum<Tx...> const &arg) noexcept
requires detail::is_superset_of<sum, sum<Tx...>>
&& (not std::is_same_v<sum, sum<Tx...>>) && (... && std::is_copy_constructible_v<Tx>)
requires detail::is_superset_of<sum, sum<Tx...>> && (not std::is_same_v<sum, sum<Tx...>>)
&& (... && std::is_copy_constructible_v<Tx>) && (sizeof...(Tx) > 0)
: data(FWD(arg).template _invoke<data_t>([]<typename T>(std::in_place_type_t<T>, auto &&v) {
return detail::make_variadic_union<T, data_t>(FWD(v));
})),
Expand All @@ -156,8 +166,8 @@ struct sum<Ts...> {

template <typename... Tx>
constexpr sum(sum<Tx...> &&arg) noexcept
requires detail::is_superset_of<sum, sum<Tx...>>
&& (not std::is_same_v<sum, sum<Tx...>>) && (... && std::is_move_constructible_v<Tx>)
requires detail::is_superset_of<sum, sum<Tx...>> && (not std::is_same_v<sum, sum<Tx...>>)
&& (... && std::is_move_constructible_v<Tx>) && (sizeof...(Tx) > 0)
: data(FWD(arg).template _invoke<data_t>([]<typename T>(std::in_place_type_t<T>, auto &&v) {
return detail::make_variadic_union<T, data_t>(FWD(v));
})),
Expand All @@ -170,6 +180,7 @@ struct sum<Ts...> {
template <typename... Tx>
constexpr sum(std::in_place_type_t<sum<Tx...>>, some_sum auto &&arg) noexcept
requires std::is_same_v<std::remove_cvref_t<decltype(arg)>, sum<Tx...>> && detail::is_superset_of<sum, sum<Tx...>>
&& (sizeof...(Tx) > 0)
: data(FWD(arg).template _invoke<data_t>([]<typename T>(std::in_place_type_t<T>, auto &&v) {
return detail::make_variadic_union<T, data_t>(FWD(v));
})),
Expand Down Expand Up @@ -335,7 +346,8 @@ template <typename T> explicit sum(T) -> sum<std::remove_cvref_t<T>>;

template <typename... Ts, typename... Tx>
[[nodiscard]] constexpr bool operator==(sum<Ts...> const &lh, sum<Tx...> const &rh) noexcept
requires(... && (std::equality_comparable<Ts> || not detail::type_one_of<Ts, Tx...>))
requires(... && (std::equality_comparable<Ts> || not detail::type_one_of<Ts, Tx...>)) //
&& ((sizeof...(Ts)) > 0) && ((sizeof...(Tx)) > 0)
{
return lh.template _invoke<bool>([&rh]<typename T>(std::in_place_type_t<T> d, auto const &lh) noexcept {
if constexpr (std::remove_cvref_t<decltype(rh)>::template has_type<T>) {
Expand All @@ -353,7 +365,8 @@ template <typename... Ts, typename... Tx>
return not(lh == rh);
}

template <typename... Ts> using sum_for = detail::normalized<Ts...>::template apply<sum>;
template <typename... Ts>
using sum_for = detail::_collapsing_sum::normalized<::fn::sum, detail::_collapsing_sum::flattened<Ts...>>::type;

} // namespace fn

Expand Down
9 changes: 5 additions & 4 deletions tests/examples/simple.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -358,10 +358,11 @@ TEST_CASE("Demo choice and graded monad", "[choice][and_then][inspect][transform

static constexpr auto fn2 = [](std::string_view hostname, std::string_view port, std::string_view filename,
std::string_view threshold) {
return (fn1(std::in_place_type<std::string_view>, hostname).sum_error() // sum_error() to introduce graded monad
& fn1(std::in_place_type<long>, port) //
& fn1(std::in_place_type<std::string_view>, filename) //
& fn1(std::in_place_type<double>, threshold) //
return (fn::expected<void, fn::sum<>>() //
& fn1(std::in_place_type<std::string_view>, hostname) //
& fn1(std::in_place_type<long>, port) //
& fn1(std::in_place_type<std::string_view>, filename) //
& fn1(std::in_place_type<double>, threshold) //
)
| fn::and_then([](std::string_view const &hostname, long port, std::string_view const &filename,
double threshold) -> fn::expected<Config, ConfigError> {
Expand Down
17 changes: 17 additions & 0 deletions tests/tests/choice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,28 @@ TEST_CASE("choice non-monadic functionality", "[choice]")
WHEN("choice_for")
{
static_assert(std::same_as<fn::choice_for<int>, fn::choice<int>>);
static_assert(std::same_as<fn::choice_for<int, int>, fn::choice<int>>);
static_assert(std::same_as<fn::choice_for<int, bool>, fn::choice<bool, int>>);
static_assert(std::same_as<fn::choice_for<bool, int>, fn::choice<bool, int>>);
static_assert(std::same_as<fn::choice_for<int, NonCopyable>, fn::choice<NonCopyable, int>>);
static_assert(std::same_as<fn::choice_for<NonCopyable, int>, fn::choice<NonCopyable, int>>);
static_assert(std::same_as<fn::choice_for<int, bool, NonCopyable>, fn::choice<NonCopyable, bool, int>>);

static_assert(std::same_as<fn::choice_for<int, fn::sum<int>>, fn::choice<int>>);
static_assert(std::same_as<fn::choice_for<int, fn::sum<bool>>, fn::choice<bool, int>>);
static_assert(std::same_as<fn::choice_for<int, fn::sum<bool, int>>, fn::choice<bool, int>>);
static_assert(std::same_as<fn::choice_for<int, fn::sum<bool, double>>, fn::choice<bool, double, int>>);

static_assert(std::same_as<fn::choice_for<fn::sum<bool>, fn::sum<int>>, fn::choice<bool, int>>);
static_assert(
std::same_as<fn::choice_for<fn::sum<bool>, fn::sum<bool, double, int>>, fn::choice<bool, double, int>>);
static_assert(std::same_as<fn::choice_for<fn::sum<bool>, fn::sum<double, int>>, fn::choice<bool, double, int>>);
static_assert(std::same_as<fn::choice_for<fn::sum<bool, int>, double>, fn::choice<bool, double, int>>);

static_assert(std::same_as<fn::choice_for<int, fn::sum<>>, fn::choice<int>>);
static_assert(std::same_as<fn::choice_for<fn::sum<>, int>, fn::choice<int>>);
static_assert(std::same_as<fn::choice_for<fn::sum<>, fn::sum<bool, int>>, fn::choice<bool, int>>);
static_assert(std::same_as<fn::choice_for<double, fn::sum<>, fn::sum<bool, int>>, fn::choice<bool, double, int>>);
}

WHEN("invocable")
Expand Down
76 changes: 76 additions & 0 deletions tests/tests/expected.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,82 @@ struct Xint {

TEST_CASE("graded monad", "[expected][sum][graded][and_then][or_else][sum_value][sum_error]")
{
WHEN("unit")
{
constexpr fn::expected<void, fn::sum<>> unit{};
static_assert(unit.has_value());

WHEN("constexpr")
{
WHEN("and_then to value/sum<>")
{
constexpr auto fn = []() -> fn::expected<int, fn::sum<>> { return {7}; };
constexpr auto a = unit.and_then(fn);
static_assert(std::is_same_v<decltype(a), fn::expected<int, fn::sum<>> const>);
static_assert(a.value() == 7);
}

WHEN("and_then to value")
{
constexpr auto fn = []() -> fn::expected<int, Error> { return {12}; };
constexpr auto a = unit.and_then(fn);
static_assert(std::is_same_v<decltype(a), fn::expected<int, fn::sum<Error>> const>);
static_assert(a.value() == 12);
}

WHEN("and_then to error")
{
constexpr auto fn = []() -> fn::expected<int, Error> { return std::unexpected<Error>(FileNotFound); };
constexpr auto a = unit.and_then(fn);
static_assert(std::is_same_v<decltype(a), fn::expected<int, fn::sum<Error>> const>);
static_assert(a.error() == fn::sum{FileNotFound});
}

WHEN("transform to int")
{
constexpr auto fn = []() -> int { return 144'000; };
constexpr auto a = unit.transform(fn);
static_assert(std::is_same_v<decltype(a), fn::expected<int, fn::sum<>> const>);
static_assert(a.value() == 144'000);
}
}

WHEN("runtime")
{
WHEN("and_then to value/sum<>")
{
constexpr auto fn = []() -> fn::expected<int, fn::sum<>> { return {7}; };
auto a = unit.and_then(fn);
static_assert(std::is_same_v<decltype(a), fn::expected<int, fn::sum<>>>);
CHECK(a.value() == 7);
}

WHEN("and_then to value")
{
constexpr auto fn = []() -> fn::expected<int, Error> { return {12}; };
auto a = unit.and_then(fn);
static_assert(std::is_same_v<decltype(a), fn::expected<int, fn::sum<Error>>>);
CHECK(a.value() == 12);
}

WHEN("and_then to error")
{
constexpr auto fn = []() -> fn::expected<int, Error> { return std::unexpected<Error>(FileNotFound); };
auto a = unit.and_then(fn);
static_assert(std::is_same_v<decltype(a), fn::expected<int, fn::sum<Error>>>);
CHECK(a.error() == fn::sum{FileNotFound});
}

WHEN("transform to int")
{
constexpr auto fn = []() -> int { return 144'000; };
auto a = unit.transform(fn);
static_assert(std::is_same_v<decltype(a), fn::expected<int, fn::sum<>>>);
CHECK(a.value() == 144'000);
}
}
}

WHEN("sum_error from sum")
{
using T = fn::expected<int, fn::sum<Error>>;
Expand Down
23 changes: 23 additions & 0 deletions tests/tests/sum_1.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,37 @@ TEST_CASE("sum basic functionality tests", "[sum]")

using fn::sum;

WHEN("sum<> unit")
{
static_assert(sum<>::size == 0);
static_assert(sum<>::has_type<bool> == false);
static_assert(std::same_as<fn::sum_for<sum<>, sum<>>, sum<>>);
}

WHEN("sum_for")
{
static_assert(std::same_as<fn::sum_for<int>, fn::sum<int>>);
static_assert(std::same_as<fn::sum_for<int, int>, fn::sum<int>>);
static_assert(std::same_as<fn::sum_for<int, bool>, fn::sum<bool, int>>);
static_assert(std::same_as<fn::sum_for<bool, int>, fn::sum<bool, int>>);
static_assert(std::same_as<fn::sum_for<int, NonCopyable>, fn::sum<NonCopyable, int>>);
static_assert(std::same_as<fn::sum_for<NonCopyable, int>, fn::sum<NonCopyable, int>>);
static_assert(std::same_as<fn::sum_for<int, bool, NonCopyable>, fn::sum<NonCopyable, bool, int>>);

static_assert(std::same_as<fn::sum_for<int, fn::sum<int>>, fn::sum<int>>);
static_assert(std::same_as<fn::sum_for<int, fn::sum<bool>>, fn::sum<bool, int>>);
static_assert(std::same_as<fn::sum_for<int, fn::sum<bool, int>>, fn::sum<bool, int>>);
static_assert(std::same_as<fn::sum_for<int, fn::sum<bool, double>>, fn::sum<bool, double, int>>);

static_assert(std::same_as<fn::sum_for<fn::sum<bool>, fn::sum<int>>, fn::sum<bool, int>>);
static_assert(std::same_as<fn::sum_for<fn::sum<bool>, fn::sum<bool, double, int>>, fn::sum<bool, double, int>>);
static_assert(std::same_as<fn::sum_for<fn::sum<bool>, fn::sum<double, int>>, fn::sum<bool, double, int>>);
static_assert(std::same_as<fn::sum_for<fn::sum<bool, int>, double>, fn::sum<bool, double, int>>);

static_assert(std::same_as<fn::sum_for<int, fn::sum<>>, fn::sum<int>>);
static_assert(std::same_as<fn::sum_for<fn::sum<>, int>, fn::sum<int>>);
static_assert(std::same_as<fn::sum_for<fn::sum<>, fn::sum<bool, int>>, fn::sum<bool, int>>);
static_assert(std::same_as<fn::sum_for<double, fn::sum<>, fn::sum<bool, int>>, fn::sum<bool, double, int>>);
}

WHEN("invocable")
Expand Down