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

Fractions functionality issue #395 #403

Merged
merged 22 commits into from
Oct 8, 2024
Merged

Conversation

SCMusson
Copy link
Contributor

Targetting issue #395.
Dunder methods for all Fractions functions.
The Fraction functions are still there, I don't know if you want to temporarily keep them for backwards compatibility or to be replaced entirely with dunder methods.

Also created Fraction.norm and Fraction.ceil class methods which could replace ceil_fraction and norm_fraction.

@nielstron
Copy link
Contributor

I would remove the old methods.

One plan for this was also that it is merged after #397, such that add mul etc have support for Union[int, Fraction].

@SCMusson
Copy link
Contributor Author

SCMusson commented Aug 23, 2024

That's fine. #397 is pretty much ready to go. I'm happy to come back to this when its merged.

@SCMusson
Copy link
Contributor Author

I had to modify a few different things so that Self in Unions was accepted.

Currently running something like:

from opshin.tests.utils import eval_uplc
from opshin.std.fractions import Fraction
a = Fraction(5,7)
b = Fraction(11,13)

source_code = """
from opshin.std.fractions import *
from typing import Dict, List, Union

def validator(a: Fraction, b: Union[Fraction, int]) -> Fraction:
    return a + b
"""
ret = eval_uplc(source_code, a,b)

Works but running eval_uplc(source_code, a, 5)

Returns the following error:

Traceback (most recent call last):
  File "/home/wppj21/Workshop/opshin/test_class.py", line 121, in <module>
    ret = eval_uplc(source_code, a, 5)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/wppj21/Workshop/opshin/opshin/tests/utils.py", line 35, in eval_uplc
    raise ret
  File "/home/wppj21/miniconda3/envs/opshin/lib/python3.11/site-packages/uplc/machine.py", line 111, in eval
    stack.append(self.return_compute(step.context, step.value))
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/wppj21/miniconda3/envs/opshin/lib/python3.11/site-packages/uplc/machine.py", line 171, in return_compute
    return self.apply_evaluate(context.ctx, context.val, value)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/wppj21/miniconda3/envs/opshin/lib/python3.11/site-packages/uplc/machine.py", line 210, in apply_evaluate
    res = eval_fun(*arguments)
          ^^^^^^^^^^^^^^^^^^^^
  File "/home/wppj21/miniconda3/envs/opshin/lib/python3.11/site-packages/uplc/ast.py", line 745, in wrapped_fun
    assert isinstance(
           ^^^^^^^^^^^
AssertionError: Argument 0 has invalid type, expected type <class 'uplc.ast.BuiltinInteger'> got <class 'uplc.ast.PlutusInteger'> (PlutusInteger(value=5))

Is this an bug in uplc or have I made an error somewhere?

@nielstron
Copy link
Contributor

There is a bug in OpShin, likely something we overlooked in #397.

When something is a Union[x,y] it is PlutusData. However for int and other primitive types I usually assume they are unwrapped into builtin data. Here there must be some missing unwrapping of int after casting it via isinstance. Note that addition can not be done with PlutusData and requires unwrapping via UInt before.

@nielstron
Copy link
Contributor

Try

def validator(x: Union[int, A]):
  if isinstance(x, int):
     print(x+2)

@nielstron
Copy link
Contributor

There is likely also another edge case in #397

def validator(x: int):
   y : Union[int, A] = x
   if isinstance(y, int):
     print("foo")

will likely crash

@OpShin OpShin deleted a comment from codereviewbot-ai bot Sep 3, 2024
@OpShin OpShin deleted a comment from codereviewbot-ai bot Sep 3, 2024
@OpShin OpShin deleted a comment from codereviewbot-ai bot Sep 3, 2024
@OpShin OpShin deleted a comment from codereviewbot-ai bot Sep 3, 2024
@OpShin OpShin deleted a comment from codereviewbot-ai bot Sep 3, 2024
@OpShin OpShin deleted a comment from codereviewbot-ai bot Sep 3, 2024
@OpShin OpShin deleted a comment from codereviewbot-ai bot Sep 3, 2024
@OpShin OpShin deleted a comment from codereviewbot-ai bot Sep 3, 2024
@OpShin OpShin deleted a comment from codereviewbot-ai bot Sep 3, 2024
@OpShin OpShin deleted a comment from codereviewbot-ai bot Sep 4, 2024
@OpShin OpShin deleted a comment from codereviewbot-ai bot Sep 4, 2024
@OpShin OpShin deleted a comment from codereviewbot-ai bot Sep 4, 2024
@OpShin OpShin deleted a comment from codereviewbot-ai bot Sep 4, 2024
@SCMusson
Copy link
Contributor Author

SCMusson commented Sep 4, 2024

Is CI not working at the moment?

@nielstron
Copy link
Contributor

Great, thanks for the update. I think Travis is currently broken, I wanted to migrate to Github actions anyways and am preparing #406 for this. Will merge it into this branch to check that the tests are now still passing.

@nielstron
Copy link
Contributor

It looks like Self is a 3.11 only feature of typing. In previous python versions this is instead solved using literal strings like "Fraction".

@SCMusson
Copy link
Contributor Author

SCMusson commented Sep 4, 2024

Ah, I'll rewrite them all as literal strings then.

@coveralls
Copy link

coveralls commented Sep 4, 2024

Coverage Status

coverage: 89.428% (+0.02%) from 89.406%
when pulling 303821b on SCMusson:feat_frac_dunder
into 6f99124 on OpShin:dev.

@nielstron
Copy link
Contributor

The fraction code looks great now. Unfortunately I still found (and added) 2 failing cases for #397.

@SCMusson
Copy link
Contributor Author

SCMusson commented Sep 4, 2024

I'll get to work on those

@SCMusson
Copy link
Contributor Author

SCMusson commented Sep 5, 2024

Thank you for those test cases, the first one I really should have thought of myself but I wouldn't have thought of the test_Union_builtin_cast_direct. I had to modify them so that the tests were correct so It might be worth you checking to make sure I didn't break the test.

@nielstron
Copy link
Contributor

Your modifications of if-expressions made me think... caught another bug :)

These don't compile, which is not ideal but better than compiling and then crashing.
@nielstron
Copy link
Contributor

Sorry about the never ending new crashing examples... the last ones added are out of scope though (since not even properly implemented for unions of plutus data), I created #408 dedicated for them. also its not a problem if an expression does not compile.

tests/test_Unions.py Outdated Show resolved Hide resolved
@nielstron
Copy link
Contributor

Any update on this? :)

@SCMusson
Copy link
Contributor Author

SCMusson commented Oct 4, 2024

Apologies, I almost forgot I was supposed to be working on this. I've got the IfExp working now

@nielstron
Copy link
Contributor

Great, thank you! This will be part of the next version 0.24.0. Please submit your wallet address for the bug bounty :)

@nielstron nielstron merged commit 26f7532 into OpShin:dev Oct 8, 2024
8 checks passed
@SCMusson
Copy link
Contributor Author

addr1q84dsy6ckn3gplf6kenwvr8mspecdlxu2mq3aj5kn02uthd93vtsr3yl4f3553sxfx7wzx58cx5pnhekvtlgqf55lw9sl4vwdy

@SCMusson
Copy link
Contributor Author

If this also signals the completion of #397 the same address can be used for the payout of that bounty too.

@nielstron
Copy link
Contributor

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