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

Delegate Rational invariant to PNonZero + optimizations #435

Merged
merged 24 commits into from
Jul 13, 2022

Conversation

TotallyNotChase
Copy link
Collaborator

The "fail on zero" invariant should be forced in the PlutusType instance - otherwise pcon is a trap for users.

The golden changes are purely within dev mode and only exist due to a slight change in the error trace.

@srid srid requested a review from Geometer1729 April 13, 2022 14:13
Geometer1729
Geometer1729 previously approved these changes Apr 13, 2022
Copy link
Member

@Geometer1729 Geometer1729 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@L-as
Copy link
Member

L-as commented Apr 13, 2022

I'm not sure this is the best solution. Shouldn't we just not export the constructors, or otherwise make the divisor (or dividend?) a Positive?

@TotallyNotChase
Copy link
Collaborator Author

I think pcon is a pretty good notion of smart constructor that already exists. Unless we require pcon to never error - I don't see a reason to not utilize it for this purpose.

We could indeed just not export the constructor but then the PlutusType instance becomes a bit useless - unless you go on to introduce "safe" helper functions to help with pmatch's handler and pcon's argument, which seems like unnecessary extra work.

@L-as
Copy link
Member

L-as commented Apr 13, 2022

The PlutusType instance would still be used internally.

in general, in Haskell, people expect constructors to be total. I don't think we should add partial constructors.

@TotallyNotChase
Copy link
Collaborator Author

TotallyNotChase commented Apr 13, 2022

Not wanting pcon to be partial is a valid concern - though I don't know how applicable it is for Plutarch usecases. Do you think we should unexport the constructor and just have 2 functions for building them (one returning PMaybe, the other being partial)?

And for deconstruction, I suppose the pnumerator and pdenominator will do? I feel like this might be a blocker for achieving maximal performance in userland (using pmatch directly in a utility function that is then hoisted will be more efficient) - but oh well.

@L-as
Copy link
Member

L-as commented Apr 13, 2022

Well that's why I suggested Positive.

@TotallyNotChase
Copy link
Collaborator Author

I assume you mean NonZero? Hmm, I could try that and see what it entails.

Plutarch/Rational.hs Outdated Show resolved Hide resolved
@TotallyNotChase TotallyNotChase changed the title Force rational invariant in PlutusType instance Delegate Rational invariant to PNonZero + optimizations Apr 15, 2022
@L-as
Copy link
Member

L-as commented Apr 16, 2022

Shouldn't it be Positive and not NonZero? I don't see why it would be negative.

@TotallyNotChase
Copy link
Collaborator Author

Shouldn't it be Positive and not NonZero? I don't see why it would be negative.

The original invariant was NonZero so far so I preserved that behavior. I don't see anything technically wrong with allowing negative denominators but we can definitely change it to Positive if necessary.

Plutarch/NonZero.hs Outdated Show resolved Hide resolved
Copy link
Member

@L-as L-as left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to find a general solution to this.

Plutarch/Rational.hs Outdated Show resolved Hide resolved
Plutarch/Rational.hs Outdated Show resolved Hide resolved
@TotallyNotChase
Copy link
Collaborator Author

I removed the constructor export and kept the PlutusType instance "normal" (no forced invariant check), and added supplementary functions for construction and deconstruction

@TotallyNotChase TotallyNotChase requested a review from L-as May 17, 2022 09:46
Plutarch/Rational.hs Outdated Show resolved Hide resolved
Plutarch/Rational.hs Outdated Show resolved Hide resolved
Plutarch/Rational.hs Outdated Show resolved Hide resolved
Plutarch/Rational.hs Outdated Show resolved Hide resolved
@L-as
Copy link
Member

L-as commented Jul 6, 2022

@TotallyNotChase Can you make this PR up to date?

@TotallyNotChase
Copy link
Collaborator Author

FYI this isn't ready yet - just syncing with staging.

Plutarch/NonZero.hs Outdated Show resolved Hide resolved
Plutarch/NonZero.hs Outdated Show resolved Hide resolved
@TotallyNotChase
Copy link
Collaborator Author

I'm trying to figure out optimization stops - where invariant checks should be skipped.

@L-as
Copy link
Member

L-as commented Jul 11, 2022

Just put them in everywhere, then we'll make it efficient later.

@TotallyNotChase
Copy link
Collaborator Author

Alright, will do optimizations later then

@TotallyNotChase
Copy link
Collaborator Author

Hmm, why did the PNonZero -> PPositive change make the benchmarks perform worse? There wasn't really any changes other than #== becoming #<= - should investigate.

@TotallyNotChase
Copy link
Collaborator Author

Hmm, why did the PNonZero -> PPositive change make the benchmarks perform worse? There wasn't really any changes other than #== becoming #<= - should investigate.

looks like the benchmarks are performed on scripts compiled with tracing mode on - that's not correct.

Copy link
Member

@L-as L-as left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We mostly just need TODOs, great work, thanks!

Plutarch/Rational.hs Outdated Show resolved Hide resolved
Plutarch/Rational.hs Show resolved Hide resolved
Plutarch/Rational.hs Show resolved Hide resolved
Plutarch/Rational.hs Outdated Show resolved Hide resolved
Plutarch/Rational.hs Show resolved Hide resolved
Plutarch/Rational.hs Show resolved Hide resolved
Plutarch/Rational.hs Outdated Show resolved Hide resolved
Plutarch/Rational.hs Outdated Show resolved Hide resolved
Plutarch/Rational.hs Outdated Show resolved Hide resolved
Copy link
Member

@L-as L-as left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@L-as L-as merged commit 1aecbb4 into staging Jul 13, 2022
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