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

Split Semiring into Semiring and Dioid #305

Open
JordanMartinez opened this issue Jul 20, 2023 · 5 comments
Open

Split Semiring into Semiring and Dioid #305

JordanMartinez opened this issue Jul 20, 2023 · 5 comments

Comments

@JordanMartinez
Copy link
Contributor

From @mikesol in Discord:

Would it make sense to revamp Semiring so that it had only the operators add and mul and then use Dioid for zero and one? There is some precedent for that: https://en.m.wikipedia.org/wiki/Semiring. It would create a nice symmetry between semigroup/monoid/group and semiring/dioid/ring. It'd be a breaking change for folks using zero and one, but otherwise I think it wouldn't be too disruptive. Also, it would allow for the generalization of semiring to an analogous biproduct class, which now isn't possible because zero and one necessarily leave out all but one of the types in the biproduct or biCCC class. Thoughts?

@JordanMartinez JordanMartinez changed the title Revamp Semiring Split Semiring into Semiring and Dioid Jul 20, 2023
@mikesol
Copy link

mikesol commented Jul 20, 2023

See also https://encyclopediaofmath.org/wiki/Idempotent_semi-ring, which is where they redirect from dioid.

@MonoidMusician
Copy link

Idempotent semi-ring is the wrong concept. We don't want something that requires idempotence.

@mikesol
Copy link

mikesol commented Jul 20, 2023

@MonoidMusician, in general, does the idea seem interesting, or do you see any pitfalls in there?

@MonoidMusician
Copy link

I'd want to see a use-case for the extra generality before making a breaking change. I personally don't have use for it.

@mikesol
Copy link

mikesol commented Jul 20, 2023

The thought came up when I was working on a shader compiler for WebGPU.

WebGPU shaders have tons of overloaded primitive operations, for example multiplying a vector by a float. That expressivity is really nice when writing shaders and is industry-standard stuff: it gets unergonomic if you can't do that because of how often you're doing vector and matrix transforms.

Obviously, mul a b in PureScript will fail if a is a Number and b is a Vec3 Number. But, I could use a custom Prelude with:

class Bisemiring a b c | a b -> c where
  add :: a -> b -> c
  mul :: a -> b -> c

instance Bisemiring Number (Vec3 Number) (Vec3 Number) where...
instance Bisemiring (Vec3 Number) Number (Vec3 Number) where...
instance Bisemiring (Vec3 Number) (Vec3 Number) (Vec3 Number) where...
instance Bisemiring Number Number Number where...

Without Dioid, it gets klunky to define one and zero. If Dioid existed separately, I could just import it and use the one and zero instances from that, but now I have to import Semiring, hide add and mul, etc.

It's a minor nit, but hopefully you can see the line of thinking that got me there.

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

No branches or pull requests

3 participants