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

addc and subc functions not implemented for U128 and I128 #2409

Closed
mfelsche opened this issue Dec 7, 2017 · 3 comments · Fixed by #2645
Closed

addc and subc functions not implemented for U128 and I128 #2409

mfelsche opened this issue Dec 7, 2017 · 3 comments · Fixed by #2645
Assignees

Comments

@mfelsche
Copy link
Contributor

mfelsche commented Dec 7, 2017

I was trying to define a generic function, that works for all of the Int types, that wraps addc. I tried the following without success:

class GenericSum[T: (Int & Integer[T] val)]
  fun _plus_safe(x: T, y: T): (T, Bool) =>
    x.addc(y)
    
  fun sum(x: T, y: T): T ? =>
    (let res, let overflow) = _plus_safe(x, y)
    if overflow then error end
    res
      
actor Main

  new create(env: Env) =>
    try
      env.out.print(GenericSum[U8].sum(1, 2)?.string())
    end

http://playground.ponylang.org/?gist=4c4e8a4001e4f360ea21d18bfa141b5e

Pony claims to be unable to find addc.

I found out that both I128 and U128 do not implement addc nor subc although they can still overflow. The only reason i see is that there is no LLVM intrinsic for those types. What would it take to implement this?

@Arceliar
Copy link

Arceliar commented Dec 7, 2017

Forgive me if I'm missing something, I've only just come across pony this week, and haven't used it outside of trivial playground examples, so I've not had occasion to see if anything works as well in practice as I'd expect.

I'm wondering if it using an interface would make more sense in this particular example. Something like:

interface ArithmeticC[T]
  fun addc(x: T): (T, Bool)
  fun subc(x: T): (T, Bool)
  fun mulc(x: T): (T, Bool)


class GenericSum[T: (Int & ArithmeticC[T] val)] // maybe just ArithmeticC[T] val
  fun _plus_safe(x: T, y: T): (T, Bool) =>
    x.addc(y)
    
  fun sum(x: T, y: T): T ? =>
    (let res, let overflow) = _plus_safe(x, y)
    if overflow then error end
    res
    
actor Main

  new create(env: Env) =>
    try
      env.out.print(GenericSum[U8].sum(1, 2)?.string())
    end

That's not to say that I128 and U128 wouldn't benefit from having these methods, I'd love to see them added if only for consistency reasons, but I think your example as originally written would also require that the Integer trait be updated to include the *c methods (unless the type system is smart enough to notice that all Ints happen to implement the required methods, but I've not seen an example of that kind of duck typing in this language yet).

@mfelsche
Copy link
Contributor Author

mfelsche commented Dec 8, 2017

Yes, my example definitely needs an interface or trait that contains the *c methods, be it Integer or any other interface like ArithmeticC. Unfortunately if i use an interface like ArithmeticC above, i run into other issues (see #2408 ) and others. While i solved my particular case right now, it is still cumbersome to be forced to such workarounds.

@SeanTAllen
Copy link
Member

@jemc has a PR for this, he just hasn't opened it yet. But, is planning on it.

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 a pull request may close this issue.

4 participants