imports
module Plutarch.Docs.PEqAndPOrd (PMaybe'(..)) where
import GHC.Generics (Generic)
import Plutarch.Prelude
PEq
& POrd
Plutarch level equality is provided by the PEq
typeclass:
class PEq t where
(#==) :: Term s t -> Term s t -> Term s PBool
PInteger
implements PEq
as you would expect. So you could do:
1 #== 2
That would yield a Term s PBool
, which you would probably use with pif
(or similar).
Similarly, POrd
emulates Ord
:
-- The actual POrd has more methods, but these are the only required ones.
class PEq => POrd t where
(#<) :: Term s t -> Term s t -> Term s PBool
(#<=) :: Term s t -> Term s t -> Term s PBool
It works as you would expect:
pif (1 #< 7) "indeed" "what"
evaluates to "indeed"
- of type Term s PString
.
For scott encoded types, you can easily derive PEq
via generic deriving:
data PMaybe' a s
= PNothing'
| PJust' (Term s a)
deriving stock Generic
deriving anyclass (PlutusType, PEq)
instance DerivePlutusType (PMaybe' a) where type DPTStrat _ = PlutusTypeScott
For data encoded types, you can derive PEq
and POrd
via their data representation:
newtype PTriplet a s
= PTriplet
( Term
s
( PDataRecord
'[ "x" ':= a
, "y" ':= a
, "z" ':= a
]
)
)
deriving stock Generic
deriving anyclass (PlutusType, PEq)
instance DerivePlutusType (PTriplet a) where type DPTStrat _ = PlutusTypeData
Aside:
PEq
derivation for data encoded types uses "Data equality". It simply ensures the structure (as represented through data encoding) of both values are exactly the same. It does not take into account any customPEq
instances for the individual fields within.