Skip to content
Open
Show file tree
Hide file tree
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
3 changes: 3 additions & 0 deletions .hlint.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,6 @@
- ignore: {name: Redundant if, within: [PlutusLedgerApi.V1.Value, PlutusLedgerApi.V1.Data.Value]}
- ignore: {name: Replace case with maybe, within: [PlutusLedgerApi.V1.Value, PlutusLedgerApi.V1.Data.Value]}
- ignore: {name: Use bimap, within: [PlutusTx.Builtins.HasOpaque]}
- ignore: {name: Use max, within: [PlutusTx.Ord.Class]}
- ignore: {name: Use min, within: [PlutusTx.Ord.Class]}
- ignore: {name: Use guards, within: [PlutusTx.Ord.Class]}
8 changes: 4 additions & 4 deletions plutus-benchmark/coop/test/9.6/certMpBurning.golden.eval
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
CPU: 1_842_390_144
Memory: 10_767_518
AST Size: 3_150
Flat Size: 8_031
CPU: 1_840_950_144
Memory: 10_758_518
AST Size: 3_111
Flat Size: 7_999

(con unit ())
4 changes: 2 additions & 2 deletions plutus-benchmark/coop/test/9.6/certMpMinting.golden.eval
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
CPU: 400_710_379
Memory: 2_029_356
AST Size: 3_150
Flat Size: 8_554
AST Size: 3_111
Flat Size: 8_522

(con unit ())
8 changes: 4 additions & 4 deletions plutus-benchmark/coop/test/9.6/fsMpBurning.golden.eval
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
CPU: 221_742_924
Memory: 1_160_875
AST Size: 3_561
Flat Size: 7_395
CPU: 220_142_924
Memory: 1_150_875
AST Size: 3_519
Flat Size: 7_362

(con unit ())
8 changes: 4 additions & 4 deletions plutus-benchmark/coop/test/9.6/fsMpMinting.golden.eval
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
CPU: 568_678_641
Memory: 3_050_867
AST Size: 3_561
Flat Size: 9_213
CPU: 562_518_641
Memory: 3_012_367
AST Size: 3_519
Flat Size: 9_180

(con unit ())
4 changes: 2 additions & 2 deletions plutus-benchmark/nofib/test/9.6/clausify-F5.golden.eval
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
CPU: 42_701_722_450
Memory: 259_362_190
AST Size: 1_429
Flat Size: 1_477
AST Size: 1_643
Flat Size: 1_653

(constr 0)
169 changes: 131 additions & 38 deletions plutus-benchmark/nofib/test/9.6/clausify-F5.golden.pir

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions plutus-benchmark/nofib/test/9.6/knights10-4x4.golden.eval
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
CPU: 1_023_470_754
Memory: 5_395_058
AST Size: 1_700
Flat Size: 1_674
CPU: 1_032_496_114
Memory: 5_446_674
AST Size: 1_887
Flat Size: 1_833

(constr 0)
256 changes: 216 additions & 40 deletions plutus-benchmark/nofib/test/9.6/knights10-4x4.golden.pir

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
### Changed

- Replace hand-written `PlutusTx.Ord` instances with `deriveOrd` for `V1.Extended` and `V3.ProtocolVersion`
1 change: 1 addition & 0 deletions plutus-ledger-api/plutus-ledger-api.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ test-suite plutus-ledger-api-test
Spec.Eq.Golden
Spec.Eval
Spec.Interval
Spec.Ord.Golden
Spec.ScriptDecodeError
Spec.V1.Data.Value
Spec.V1.Value
Expand Down
11 changes: 1 addition & 10 deletions plutus-ledger-api/src/PlutusLedgerApi/V1/Interval.hs
Original file line number Diff line number Diff line change
Expand Up @@ -239,16 +239,7 @@ instance Pretty a => Pretty (LowerBound a) where
pretty (LowerBound a False) = pretty "(" <+> pretty a

deriveEq ''Extended

instance Ord a => Ord (Extended a) where
{-# INLINEABLE compare #-}
NegInf `compare` NegInf = EQ
NegInf `compare` _ = LT
_ `compare` NegInf = GT
PosInf `compare` PosInf = EQ
_ `compare` PosInf = LT
PosInf `compare` _ = GT
Finite l `compare` Finite r = l `compare` r
deriveOrd ''Extended

instance Eq a => Haskell.Eq (Extended a) where
(==) = (PlutusTx.==)
Expand Down
1 change: 1 addition & 0 deletions plutus-ledger-api/src/PlutusLedgerApi/V3/Contexts.hs
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,7 @@ data ProtocolVersion = ProtocolVersion
deriving anyclass (HasBlueprintDefinition)

PlutusTx.deriveEq ''ProtocolVersion
PlutusTx.deriveOrd ''ProtocolVersion

instance Pretty ProtocolVersion where
pretty ProtocolVersion {..} =
Expand Down
2 changes: 2 additions & 0 deletions plutus-ledger-api/test/Spec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import Spec.Data.Versions qualified
import Spec.Eq.Golden qualified
import Spec.Eval qualified
import Spec.Interval qualified
import Spec.Ord.Golden qualified
import Spec.ScriptDecodeError qualified
import Spec.V1.Data.Value qualified as Data.Value
import Spec.V1.Value qualified as Value
Expand Down Expand Up @@ -225,4 +226,5 @@ tests =
]
]
, Spec.Eq.Golden.eqGoldenTests
, Spec.Ord.Golden.ordGoldenTests
]
29 changes: 29 additions & 0 deletions plutus-ledger-api/test/Spec/Ord/Golden.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE TypeApplications #-}

{-| Golden tests for deriveOrd instances in plutus-ledger-api.
These tests capture the exact generated code for types where manual Ord instances
were replaced with deriveOrd, providing confidence that derived instances match
the original manual implementations. -}
module Spec.Ord.Golden (ordGoldenTests) where

import PlutusTx.Ord (deriveOrd)
import PlutusTx.Test.Golden (goldenCodeGen)
import Test.Tasty (TestTree)
import Test.Tasty.Extras (runTestNested)

-- V1 types
import PlutusLedgerApi.V1.Interval qualified as V1

-- V3 types
import PlutusLedgerApi.V3.Contexts qualified as V3

ordGoldenTests :: TestTree
ordGoldenTests =
runTestNested
["test", "Spec", "Ord", "Golden"]
[ -- V1 types
$(goldenCodeGen "V1.Extended" (deriveOrd ''V1.Extended))
, -- V3 types
$(goldenCodeGen "V3.ProtocolVersion" (deriveOrd ''V3.ProtocolVersion))
]
16 changes: 16 additions & 0 deletions plutus-ledger-api/test/Spec/Ord/Golden/V1.Extended.golden.th
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
instance PlutusTx.Ord.Class.Ord a_0 => PlutusTx.Ord.Class.Ord PlutusLedgerApi.V1.Interval.Extended
a_0 where
PlutusTx.Ord.Class.compare
(PlutusLedgerApi.V1.Interval.NegInf)
(PlutusLedgerApi.V1.Interval.NegInf) = GHC.Types.EQ
PlutusTx.Ord.Class.compare
(PlutusLedgerApi.V1.Interval.Finite l1l_0)
(PlutusLedgerApi.V1.Interval.Finite r1r_0) = l1l_0 PlutusTx.Ord.Class.compare r1r_0
PlutusTx.Ord.Class.compare
(PlutusLedgerApi.V1.Interval.PosInf)
(PlutusLedgerApi.V1.Interval.PosInf) = GHC.Types.EQ
PlutusTx.Ord.Class.compare (PlutusLedgerApi.V1.Interval.NegInf {}) _ = GHC.Types.LT
PlutusTx.Ord.Class.compare _ (PlutusLedgerApi.V1.Interval.NegInf {}) = GHC.Types.GT
PlutusTx.Ord.Class.compare (PlutusLedgerApi.V1.Interval.Finite {}) _ = GHC.Types.LT
PlutusTx.Ord.Class.compare _ (PlutusLedgerApi.V1.Interval.Finite {}) = GHC.Types.GT
{-# INLINABLE PlutusTx.Ord.Class.compare #-}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
instance PlutusTx.Ord.Class.Ord PlutusLedgerApi.V3.Contexts.ProtocolVersion where
PlutusTx.Ord.Class.compare
(PlutusLedgerApi.V3.Contexts.ProtocolVersion l1l_0 l2l_0)
(PlutusLedgerApi.V3.Contexts.ProtocolVersion r1r_0 r2r_0) = (l1l_0
PlutusTx.Ord.Class.compare r1r_0) PlutusTx.Ord.Class.thenCmp (l2l_0
PlutusTx.Ord.Class.compare r2r_0)
{-# INLINABLE PlutusTx.Ord.Class.compare #-}
4 changes: 4 additions & 0 deletions plutus-tx/changelog.d/20251203_141812_bezirg_derive_ord.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
### Added

- A `deriveOrd` command to derive PlutusTx.Ord instances for datatypes/newtypes, similar to Haskell's
`deriving stock Ord`
3 changes: 3 additions & 0 deletions plutus-tx/plutus-tx.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ library
PlutusTx.Optimize.Inline
PlutusTx.Optimize.SpaceTime
PlutusTx.Ord
PlutusTx.Ord.Class
PlutusTx.Plugin.Utils
PlutusTx.Prelude
PlutusTx.Ratio
Expand All @@ -132,6 +133,7 @@ library
PlutusTx.Lift.TestInstances
PlutusTx.Lift.TH
PlutusTx.Lift.THUtils
PlutusTx.Ord.TH

build-depends:
, aeson >=2.2
Expand Down Expand Up @@ -221,6 +223,7 @@ test-suite plutus-tx-test
Enum.Spec
Eq.Spec
List.Spec
Ord.Spec
Rational.Laws
Rational.Laws.Additive
Rational.Laws.Construction
Expand Down
5 changes: 5 additions & 0 deletions plutus-tx/src/PlutusTx/Monoid.hs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import PlutusTx.Base (id)
import PlutusTx.Builtins qualified as Builtins
import PlutusTx.List
import PlutusTx.Maybe
import PlutusTx.Ord
import PlutusTx.Semigroup

{- HLINT ignore -}
Expand Down Expand Up @@ -68,6 +69,10 @@ instance Monoid (First a) where
{-# INLINEABLE mempty #-}
mempty = First Nothing

instance Monoid Ordering where
{-# INLINEABLE mempty #-}
mempty = EQ

class Monoid a => Group a where
inv :: a -> a

Expand Down
Loading