hmemcpy / milewski-ctfp-pdf

Bartosz Milewski's 'Category Theory for Programmers' unofficial PDF and LaTeX source
https://bartoszmilewski.com/2014/10/28/category-theory-for-programmers-the-preface/
Other
10.84k stars 575 forks source link

Update 1.3's C++ concept implementation of Monoid #259

Closed rosds closed 3 years ago

hmemcpy commented 3 years ago

Will gonna need a ruling here. @BartoszMilewski, WDYT?

BartoszMilewski commented 3 years ago

Looks good. I haven't been following C++. Is there a compiler that could type check this code?

rosds commented 3 years ago

Hi :) I could compile with the latest gcc. The only feature that is still not yet supported by gcc is the constexpr std::string. But it is in the C++20 standard so hopefully it will be supported soon enough.

here is the version with a string literal instead of std::string: https://godbolt.org/z/nnaEs6

BartoszMilewski commented 3 years ago

I asked my friend Eric Niebler for comments. This is what he said.

\begin{snip}{cpp} template struct mempty : std::false_type {};

I would probably just forward-declare this but leave the primary template undefined.

template using mempty_v = mempty::value;

Probably you don't want to use a class static here. Not all types have a constexpr default constructor, and you don't want to introduce order-of-initialization issues. Maybe a static member function named value() that returns the empty value.

template T mappend(T, T) = delete;

template concept Monoid = requires (M m) { { mempty_v } -> std::convertible_to;

If you went with a static member function, this would be:

{ mempty::value() } -> std::convertible_to;

And see below about "same_as".

{ mappend(m, m) } -> std::convertible_to<M>;

};

I would strengthen "convertible_to" to "same_as". There's no reason not to turn the screws down tightly here.

rosds commented 3 years ago

Thanks for the comments :) I have to agree with that

hmemcpy commented 3 years ago

So what's the verdict? Can I merge this? :)

BartoszMilewski commented 3 years ago

If we can incorporate Eric's comments, then yes.

rosds commented 3 years ago

If we can incorporate Eric's comments, then yes.

I'd incorporate them some days ago, just have a look :)

BartoszMilewski commented 3 years ago

Cool! Thanks!