-
Notifications
You must be signed in to change notification settings - Fork 64
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
PPair instances: PPartialOrd and POrd #572
Open
cjay
wants to merge
1
commit into
Plutonomicon:master
Choose a base branch
from
cjay:ppair-instances
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,15 @@ | ||
module Plutarch.Pair (PPair (..)) where | ||
|
||
import GHC.Generics (Generic) | ||
import Plutarch.Bool (PEq) | ||
import Plutarch.Bool (PEq ((#==)), POrd, PPartialOrd ((#<), (#<=)), pif) | ||
import Plutarch.Internal (PType, S, Term) | ||
import Plutarch.Internal.PlutusType (DPTStrat, DerivePlutusType, PlutusType) | ||
import Plutarch.Internal.PlutusType (DPTStrat, DerivePlutusType, PlutusType, pmatch) | ||
import Plutarch.Internal.ScottEncoding (PlutusTypeScott) | ||
import Plutarch.Show (PShow) | ||
import Plutarch.TermCont (tcont, unTermCont) | ||
|
||
{- | | ||
Plutus encoding of Pairs. | ||
|
||
Note: This is represented differently than 'BuiltinPair'. It is scott-encoded. | ||
-} | ||
data PPair (a :: PType) (b :: PType) (s :: S) | ||
|
@@ -18,3 +18,24 @@ data PPair (a :: PType) (b :: PType) (s :: S) | |
deriving anyclass (PlutusType, PEq, PShow) | ||
|
||
instance DerivePlutusType (PPair a b) where type DPTStrat _ = PlutusTypeScott | ||
|
||
instance (PPartialOrd a, PPartialOrd b) => PPartialOrd (PPair a b) where | ||
a #<= b = unTermCont $ do | ||
PPair a1 a2 <- tcont $ pmatch a | ||
PPair b1 b2 <- tcont $ pmatch b | ||
pure $ | ||
pif | ||
(a1 #== b1) | ||
(a2 #<= b2) | ||
(a1 #<= b1) | ||
|
||
a #< b = unTermCont $ do | ||
PPair a1 a2 <- tcont $ pmatch a | ||
PPair b1 b2 <- tcont $ pmatch b | ||
pure $ | ||
pif | ||
(a1 #== b1) | ||
(a2 #< b2) | ||
(a1 #< b1) | ||
|
||
instance (POrd a, POrd b) => POrd (PPair a b) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you can just add this to |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not derive this
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I couldn't find infrastructure for deriving that. Is there any?
Sorry, I haven't really had time to learn all possibilities of deriving-via yet.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can derive all classes that have a default for every method.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The default needs an
POrd
instance forPInner
of the type, which in the case ofPPair
isPScottEncoded ...
. That one doesn't have aPOrd
instance. Are you sure this should be possible somehow?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is an issue. Look at the default methods. You can change the constraint to PPartialOrd, but what about deriving POrd? You can add the constraint as a superclass, but perhaps adding a dummy method with a POrd constraint on PInner works best.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What mechanism should cause the lexical ordering behavior that you see in my manual implementation? I don't see how changing constraints would help with that. I suppose a better default implementation would have to use generics.