Skip to content

Conversation

@mgeier
Copy link
Contributor

@mgeier mgeier commented Jan 18, 2024

This PR contains two orthogonal changes:

  • the code is simplified by introducing an additional type definition per operator
  • the impls themselves are simplified

I combined the two because the second is easier to do in the simplified code after the first change.

I found out that the impls can be simplified by reading https://github.com/willcrichton/tyrade/blob/master/src/tnum.rs. I extended that idea to the other operators as well.

@fprasx
Copy link
Owner

fprasx commented Jan 18, 2024

I'm not looking to change main.rs, but if you'd like to add this implementation in another file (with proper attribution to yourself and willcrichton), I'm happy to merge.

@mgeier
Copy link
Contributor Author

mgeier commented Jan 18, 2024

I'm not looking to change main.rs

No problem, don't hesitate to close this PR without merging.

I just wanted to preserve this for my own future reference, for which this PR is sufficient.


For the record:

This reduces the "monstrosity" mentioned in https://fprasx.github.io/articles/type-system-arithmetic/ to:

type RawQuotient<T, U> = Add<Succ<Zero>, Div<Sub<Succ<T>, Succ<U>>, Succ<U>>>;

impl<T, U> DivImpl for (Succ<T>, Succ<U>)
where
    (T, U): SubImpl,
    (Sub<T, U>, Succ<U>): DivImpl,
    (Succ<Zero>, Div<Sub<T, U>, Succ<U>>): AddImpl,
    (
        GreaterThanEq<T, U>,
        Add<Succ<Zero>, Div<Sub<T, U>, Succ<U>>>,
    ): MulImpl,
    (T, U): GreaterThanEqImpl,
{
    // If x < y, return 0. We can do this by multiplying the "RawQuotient" by
    // the bool->int value of this condition.
    type Output = Mul<GreaterThanEq<Succ<T>, Succ<U>>, RawQuotient<T, U>>;
}

It's still quite a mouthful, but I think better than before and I think it is now short enough to inline RawQuotient, which unnecessarily confused me when first reading the blog post.

A more concise implementation is available at fprasx/fprasx.github.io#1 (comment).

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.

2 participants