Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs(ref): document mp_units.core #628

Draft
wants to merge 180 commits into
base: master
Choose a base branch
from
Draft

Conversation

JohelEGP
Copy link
Collaborator

@JohelEGP JohelEGP commented Oct 18, 2024

This is still a WIP:

This is also blocked on
a number of minor points which I'll review later.

There are some TBDs, mainly for algorithms which need wording.
These are:

  • The quantity specification conversion algorithms.
  • The collapse common unit algorithms.
  • The formatting functions for dimensions and units.
  • The semantic requirements on the QuantityCharacterRepresentation concepts.
  • sudo_cast.

Work left (besides the TBDs above):

  • Text output.
  • Less priority:
    • system_reference
    • math.h
    • random.h

@mpusz
Copy link
Owner

mpusz commented Oct 19, 2024

This is great! Thanks!

Regarding magnitudes, please note that they are meant to be implementation-defined. Only a few user-facing API things are public (construction helpers (mag, mag_ratio, and mag_power), concept, operators, magnitude<...> where ... is implementation-defined).

Copy link
Owner

@mpusz mpusz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is huge!!! Lots of great work! Thank you!!!

docs/api_reference/src/quantities.tex Outdated Show resolved Hide resolved
docs/api_reference/src/quantities.tex Outdated Show resolved Hide resolved
docs/api_reference/src/quantities.tex Outdated Show resolved Hide resolved
docs/api_reference/src/quantities.tex Outdated Show resolved Hide resolved
docs/api_reference/src/quantities.tex Outdated Show resolved Hide resolved
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How can we specify that some public identifiers should not be exported from the std module?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reading https://wg21.link/std.modules, it seems like there's no way.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will ask LWG members

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dropping in mid-stream here -- is there a problem with marking them as exposition only? That basically says -- we're going to act like this thing exists in the standard, but we might implement it differently or call it something different.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

exposition-only is not a solution, as I wrote in the LWG reflector.

\label{term.quantity.type}
A \defnadj{quantity}{type}
is a type \tcode{\placeholder{Q}}
that is a specialization of \tcode{quantity} or \tcode{quantity_point}.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are some users that derive from quantity already. We support this in mp-units with derived_from. Should we remove this support?

This comment was marked as resolved.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we remove this support?

I've always been against it.
Do you have the related issue/PR numbers?
I hope to convince you to do that with my TBD review here.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is controversial for me as well. In C++ we typically discourage people from inheriting from std types. It would be great if we could find a better solution here.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The most glaring issue is that for a parameter constrained with
std::derived_from<quantity> or std::derived_from<quantity_point>,
the implementation doesn't convert to the quantity base subobject.

For example, in operator+:

  template<std::derived_from<quantity> Q, auto R2, typename Rep2>
    requires detail::CommonlyInvocableQuantities<std::plus<>, quantity, quantity<R2, Rep2>>
  [[nodiscard]] friend constexpr Quantity auto operator+(const Q& lhs, const quantity<R2, Rep2>& rhs)
  {
    using ret = detail::common_quantity_for<std::plus<>, quantity, quantity<R2, Rep2>>;
    const ret ret_lhs(lhs);

We need this conversion from Q, type of lhs, to ret,
to be the same as ret(static_cast<const quantity&>(lhs)).
The implementation doesn't do that (like as-variant),
so we need wording to require those parameter types
to not hide members
and prevent conversions like the above to have a different meaning.
Perhaps by requiring Q to behave as-if the base template in the specified wording
(e.g., by borrowing https://eel.is/c++draft/namespace.std#2.2).

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do not oppose removing the support for deriving from a quantity.

docs/api_reference/src/quantities.tex Outdated Show resolved Hide resolved
docs/api_reference/src/quantities.tex Outdated Show resolved Hide resolved
docs/api_reference/src/quantities.tex Outdated Show resolved Hide resolved
@JohelEGP JohelEGP force-pushed the ref_docs branch 5 times, most recently from 05ab0a8 to 8bf5db5 Compare October 23, 2024 16:15
@mpusz
Copy link
Owner

mpusz commented Oct 23, 2024

Could you please not squash or rebase the branch?

It is impossible to track what changes between commits this way. I need to start reviewing all 2500 lines from scratch as I do not know what has changed.

Maybe provide something for merge and open another PR with additional changes?

@JohelEGP

This comment was marked as outdated.

@mpusz
Copy link
Owner

mpusz commented Oct 24, 2024

I try to review in LatTeX as it is easier to provide comments that way, and I also need to learn the syntax.

docs/api_reference/src/quantities.tex Outdated Show resolved Hide resolved
docs/api_reference/src/quantities.tex Outdated Show resolved Hide resolved
docs/api_reference/src/quantities.tex Show resolved Hide resolved
docs/api_reference/src/quantities.tex Outdated Show resolved Hide resolved
docs/api_reference/src/quantities.tex Outdated Show resolved Hide resolved
docs/api_reference/src/quantities.tex Show resolved Hide resolved
docs/api_reference/src/quantities.tex Outdated Show resolved Hide resolved
docs/api_reference/src/quantities.tex Outdated Show resolved Hide resolved
docs/api_reference/src/quantities.tex Outdated Show resolved Hide resolved
Comment on lines 661 to 662
requires(sizeof...(Den) <= 1 && @\exposidnc{valid-ratio}@(Num, Den...) && @\exposidnc{positive-ratio}@(Num, Den...) &&
!@\exposidnc{ratio-one}@(Num, Den...))
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you plan also to provide valid-ratio, positive-ratio, and ratio-one.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or better.
This is part of:

This is also blocked on [...]
and a number of minor points which I'll review later.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For now, I fixed this with commit 6d64c98.

docs/api_reference/src/quantities.tex Outdated Show resolved Hide resolved
docs/api_reference/src/quantities.tex Outdated Show resolved Hide resolved
docs/api_reference/src/quantities.tex Outdated Show resolved Hide resolved
mpusz
mpusz previously approved these changes Nov 25, 2024
docs/api_reference/src/quantities.tex Show resolved Hide resolved
Comment on lines 11 to 24
\ref{qty.helpers} & Helpers & \tcode{mp_units.core} \\
\ref{qty.traits} & Traits & \\
\ref{qty.concepts} & Concepts & \\
\ref{qty.types} & Types & \\
\ref{qty.compat} & Compatibility & \\
\ref{qty.one} & Dimension one & \\ \rowsep
\ref{qty.expr.temp} & Expression template & \\
\ref{qty.dim} & Dimension & \\
\ref{qty.spec} & Quantity specification & \\
\ref{qty.unit.mag} & Unit magnitude & \\
\ref{qty.unit} & Unit & \\
\ref{qty.ref} & Reference & \\
\ref{qty.rep} & Representation & \\
\ref{qty.traits} & Quantity traits & \\
\ref{qty} & Class template \tcode{quantity} & \\
\ref{qty.pt.orig} & Point origin & \\
\ref{qty.pt} & Class template \tcode{quantity_point} & \\ \rowsep
\ref{qty.systems} & Systems & \tcode{mp_units.systems} \\
\ref{qty.chrono} & \tcode{std::chrono} compatibility & \\
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1732497489
Right now, [qties] has many top-level subclauses (.18).
And we're still missing the specification of the module mp_units.system.
It currently beats the C++ Standard library Clauses', of which these have the most:
[support] (.13), [utilities] (.16), [algorithms] (.12), [time] (.15), [input.output] (.13), [re] (.12), and [thread] (.11).

I think we should try to leave the top-level subclauses
for the domain and programming entities
that are most indispensable for the general problem but not our particular solution.
That'd mean moving 5.6 "Expression template" under 5.5 "Helpers"
(though indispensable to our particular solution,
it's not for the general problem of a "quantities and units library").
Also moving 5.9 "Unit magnitude" under 5.10 "Unit"
(in the theory, a unit is a quantity chosen by convention.
The unit magnitude here is a programming concept for the compile-time benefits).

It should be possible to dissolve 5.13 "Quantity values".
quantity_values can be moved under 5.12 "Representation".
And if it isn't added under ::std, it can then be renamed to representation_values
(e.g., ::std::units::representation_values).
QuantityLike and QuantityPointLike can then be moved
next to quantity and quantity_point, respectively.
Also, shouldn't these concepts be exposition only?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, "Unit magnitude" should be under "Unit".

Expression template probably as well. We may also need to rename it, as some people already objected to the use of this term for our purposes. The general-purpose expression template library would have totally different interfaces and rules. Ours is DSL (Domain Specific) version of expression templates.

Also, shouldn't these concepts be exposition only?

Initially, I thought it might be useful for a user to assert if a custom type satisfies those requirements. This is also why RepresentationOf still takes quantity_character explicitly (even though not used by the framework anywhere). If you think it is not a good enough reason to make it public, we can convert it to an exposition-only concept.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Expression template probably as well. We may also need to rename it, as some people already objected to the use of this term for our purposes. The general-purpose expression template library would have totally different interfaces and rules. Ours is DSL (Domain Specific) version of expression templates.

To me, it seems more like a computer algebra system.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should be possible to dissolve 5.13 "Quantity values".
quantity_values can be moved under 5.12 "Representation".
And if it isn't added under ::std, it can then be renamed to representation_values
(e.g., ::std::units::representation_values).
QuantityLike and QuantityPointLike can then be moved
next to quantity and quantity_point, respectively.

You can already do this rename, though.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To me, it seems more like a computer algebra system.

I am not sure. It does not feel like a calculator 😉

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about 5.6 "Symbolic computations"?

Comment on lines +650 to +651
template<typename T, template<@\seebelow@> typename U>
consteval bool @\exposidnc{is-derived-from-specialization-of}@(); // \expos
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mpusz Please, try replacing all the "is derived from specialization of" variable templates with overloaded function templates.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried, but it seems that clang-18 and below are broken 😢
https://godbolt.org/z/GPEMq6TKM

friend consteval @\exposidnc{ratio}@ operator/(@\exposidnc{ratio}@ lhs, @\exposidnc{ratio}@ rhs) { return lhs* @\exposidnc{ratio}@{rhs.den, rhs.num}; }
};

consteval bool is_integral(@\exposidnc{ratio}@ r) { return r.num % r.den == 0; }
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, were you referring to is_integral?
I thought we were talking about ratio.
is_integral is currently unused, as is common_ratio.
If those are not used after filling in the TBDs, they should be removed.

Otherwise, yeah, maybe they should be exposition only.

Let \tcode{lhs} be \tcode{utf8} for the last two constructors,
and otherwise:
\begin{codeblock}
std::bit_cast<std::fixed_u8string<N>>(std::basic_fixed_string(portable))
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm... I don't remember them changing the char type and its literals to be ASCII/UTF-8 compatible.
I just checked https://en.cppreference.com/w/cpp/language/types#Character_types
and https://en.cppreference.com/w/cpp/language/character_literal#Explanation.
The latter points to https://en.cppreference.com/w/cpp/language/charset#Code_unit_and_literal_encoding,
which hasn't changed since C++98 and still says

The ordinary and wide literal encodings are otherwise implementation-defined.

Anyways, I'll leave it as std::bit_cast.
Tell me if you'd prefer it to be changed to std::ranges::to.

requires @\seebelownc@
struct @\libglobal{power}@ final {
using factor = F;
static constexpr @\exposidnc{ratio}@ exponent{Num, Den...};
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think exponent should be exposition only,
given that ratio is exposition only.
We should also make factor exposition only,
as we only care that the names per and power are not exposition only.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, both factor and exponent should be exposition-only. We just want to reserve the class name so the implementations have a similar look and feel without mandating any particular implementation.

docs/api_reference/src/quantities.tex Show resolved Hide resolved
docs/api_reference/src/quantities.tex Outdated Show resolved Hide resolved
Comment on lines +1102 to +1105
template<std::intmax_t Num, std::intmax_t Den, template<typename...> typename To,
typename OneType, template<typename, typename> typename Pred = @\exposidnc{type-less}@, typename T>
requires(Den != 0)
consteval auto @\exposidnc{expr-pow}@(T); // \expos
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it OK to use (Den != 0) instead of detail::non_zero<Den>?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure! Someone added those math concepts but I also consider them often more an issue than help. If you are OK with that, I can remove them from the code and replace with proper expressions.

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.

3 participants