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

PPair instances: PPartialOrd and POrd #572

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 24 additions & 3 deletions Plutarch/Pair.hs
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)
Expand All @@ -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
Copy link
Member

Choose a reason for hiding this comment

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

Why not derive this

Copy link
Collaborator Author

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.

Copy link
Member

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.

Copy link
Collaborator Author

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 for PInner of the type, which in the case of PPair is PScottEncoded .... That one doesn't have a POrd instance. Are you sure this should be possible somehow?

Copy link
Member

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.

Copy link
Collaborator Author

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.

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)
Copy link
Member

Choose a reason for hiding this comment

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

you can just add this to deriving anyclass