Skip to content

Commit eb17481

Browse files
bezirgUnisay
andauthored
Add deriveEq for Plinth similar to deriving stock Eq (#7433)
* feat(plutus-tx): add deriveEq Template Haskell function Extract the Eq typeclass into PlutusTx.Eq.Class and add a new PlutusTx.Eq.TH module providing deriveEq, a TH function that generates PlutusTx.Eq instances analogous to GHC's `deriving stock Eq`. This enables replacing hand-written structural equality instances with a single `deriveEq ''TypeName` call, reducing boilerplate and ensuring consistent pattern-matching with short-circuit evaluation and INLINABLE pragmas across the codebase. * test(plutus-tx): add unit and golden tests for deriveEq Add comprehensive tests covering product types, sum types, newtypes, records, void types, phantom types, and recursive types. Golden tests capture exact TH-generated code to detect regressions. Fix goldenCodeGen to use `pretty @String` for proper Doc type conversion needed by nestedGoldenVsDoc. * refactor(plutus-tx): replace manual Eq instances with deriveEq Replace hand-written Eq instances for Rational and These with deriveEq calls. The generated instances are structurally identical to the manual ones. * refactor(plutus-ledger-api): replace manual Eq instances with deriveEq Replace hand-written PlutusTx.Eq instances across all V1, V2, and V3 modules (both Original and Data representations) with deriveEq calls. MintValue retains its manual instance because its equality semantics differ from structural equality (it normalizes zero-quantity entries). * test: add golden tests for deriveEq generated code Add golden TH tests capturing the exact generated Eq instances for every type where manual instances were replaced with deriveEq. This covers 42 types in plutus-ledger-api (V1/V2/V3, Original and Data variants) and 2 types in plutus-tx (Rational, These). These tests will detect any unintended changes to the generated equality code. * test: regenerate golden files affected by deriveEq Variable naming in deriveEq-generated code differs from manual instances, causing golden file updates in plutus-benchmark and plutus-tx-plugin test suites. * docs: add changelog entries for deriveEq feature Add entries to plutus-tx and plutus-ledger-api changelogs documenting the new deriveEq function and the migration from manual Eq instances. * test: update AST size golden for Rational.compare after deriveEq changes * test: make deriveEq golden tests deterministic for asData types Strip TH-generated unique name suffixes (7+ digits) from golden test output for Data-variant types (V*D.*), which use TH.newName via asData and produce non-deterministic constructor names across compilations. * test: regenerate golden files for cardano-constitution and plutus-benchmark * fix: disable plutus-ledger-api-test on Windows (needs diff) The golden tests added by deriveEq require the 'diff' binary which is not available in the Windows (mingW64) cross-compilation environment. * refactor: replace TH.pprint with custom width-aware pretty-printer Replace formatTHOutput + TH.pprint with a new PlutusTx.Test.THPretty module that uses prettyprinter's Wadler/Lindig algorithm with 100-column page width. This produces much more readable golden test output -- lines that previously stretched 200+ chars now wrap intelligently. * style: remove commented-out deriveEq calls for AssocMap types Replace commented-out deriveEq code with plain comments explaining why PlutusTx.Eq instances are absent, addressing review feedback. * style: add Haddock markers to MintValue Eq instance comments --------- Co-authored-by: Yuriy Lazaryev <yuriy.lazaryev@iohk.io>
1 parent a6201d1 commit eb17481

File tree

146 files changed

+6812
-7727
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

146 files changed

+6812
-7727
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
2045
1+
2042
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
ExBudget {exBudgetCPU = ExCPU 406467137, exBudgetMemory = ExMemory 2070956}
1+
ExBudget {exBudgetCPU = ExCPU 406371137, exBudgetMemory = ExMemory 2070356}

cardano-constitution/test/Cardano/Constitution/Validator/Data/GoldenTests/sorted.golden.pir

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -247,37 +247,40 @@ program
247247
Rational_match
248248
ds
249249
{bool}
250-
(\(n : integer) (d : integer) ->
250+
(\(l1l : integer) (l2l : integer) ->
251251
Rational_match
252252
ds
253253
{bool}
254-
(\(n' : integer) (d' : integer) ->
254+
(\(r1r : integer) (r2r : integer) ->
255255
case
256256
(all dead. bool)
257-
(equalsInteger n n')
257+
(equalsInteger l1l r1r)
258258
[ (/\dead -> False)
259-
, (/\dead -> equalsInteger d d') ]
259+
, (/\dead -> equalsInteger l2l r2r) ]
260260
{all dead. dead})))
261-
(\(ds : Rational) (ds : Rational) ->
261+
(\(eta : Rational) (eta : Rational) ->
262262
Rational_match
263-
ds
263+
eta
264264
{Ordering}
265-
(\(n : integer) (d : integer) ->
265+
(\(ipv : integer) (ipv : integer) ->
266266
Rational_match
267-
ds
267+
eta
268268
{Ordering}
269-
(\(n' : integer) (d' : integer) ->
270-
let
271-
!x : integer = multiplyInteger n d'
272-
!y : integer = multiplyInteger n' d
273-
in
269+
(\(ipv : integer) (ipv : integer) ->
274270
case
275271
(all dead. Ordering)
276-
(equalsInteger x y)
272+
(case
273+
(all dead. bool)
274+
(equalsInteger ipv ipv)
275+
[ (/\dead -> False)
276+
, (/\dead -> equalsInteger ipv ipv) ]
277+
{all dead. dead})
277278
[ (/\dead ->
278279
case
279280
(all dead. Ordering)
280-
(lessThanEqualsInteger x y)
281+
(lessThanEqualsInteger
282+
(multiplyInteger ipv ipv)
283+
(multiplyInteger ipv ipv))
281284
[(/\dead -> GT), (/\dead -> LT)]
282285
{all dead. dead})
283286
, (/\dead -> EQ) ]
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
ExBudget {exBudgetCPU = ExCPU 60269664, exBudgetMemory = ExMemory 276518}
1+
ExBudget {exBudgetCPU = ExCPU 60173664, exBudgetMemory = ExMemory 275918}

cardano-constitution/test/Cardano/Constitution/Validator/Data/GoldenTests/sorted.golden.uplc

Lines changed: 823 additions & 827 deletions
Large diffs are not rendered by default.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
2036
1+
2044
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
ExBudget {exBudgetCPU = ExCPU 568831992, exBudgetMemory = ExMemory 2943891}
1+
ExBudget {exBudgetCPU = ExCPU 568927992, exBudgetMemory = ExMemory 2944491}

cardano-constitution/test/Cardano/Constitution/Validator/Data/GoldenTests/unsorted.golden.pir

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -245,37 +245,40 @@ program
245245
Rational_match
246246
ds
247247
{bool}
248-
(\(n : integer) (d : integer) ->
248+
(\(l1l : integer) (l2l : integer) ->
249249
Rational_match
250250
ds
251251
{bool}
252-
(\(n' : integer) (d' : integer) ->
252+
(\(r1r : integer) (r2r : integer) ->
253253
case
254254
(all dead. bool)
255-
(equalsInteger n n')
255+
(equalsInteger l1l r1r)
256256
[ (/\dead -> False)
257-
, (/\dead -> equalsInteger d d') ]
257+
, (/\dead -> equalsInteger l2l r2r) ]
258258
{all dead. dead})))
259-
(\(ds : Rational) (ds : Rational) ->
259+
(\(eta : Rational) (eta : Rational) ->
260260
Rational_match
261-
ds
261+
eta
262262
{Ordering}
263-
(\(n : integer) (d : integer) ->
263+
(\(ipv : integer) (ipv : integer) ->
264264
Rational_match
265-
ds
265+
eta
266266
{Ordering}
267-
(\(n' : integer) (d' : integer) ->
268-
let
269-
!x : integer = multiplyInteger n d'
270-
!y : integer = multiplyInteger n' d
271-
in
267+
(\(ipv : integer) (ipv : integer) ->
272268
case
273269
(all dead. Ordering)
274-
(equalsInteger x y)
270+
(case
271+
(all dead. bool)
272+
(equalsInteger ipv ipv)
273+
[ (/\dead -> False)
274+
, (/\dead -> equalsInteger ipv ipv) ]
275+
{all dead. dead})
275276
[ (/\dead ->
276277
case
277278
(all dead. Ordering)
278-
(lessThanEqualsInteger x y)
279+
(lessThanEqualsInteger
280+
(multiplyInteger ipv ipv)
281+
(multiplyInteger ipv ipv))
279282
[(/\dead -> GT), (/\dead -> LT)]
280283
{all dead. dead})
281284
, (/\dead -> EQ) ]
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
ExBudget {exBudgetCPU = ExCPU 58587921, exBudgetMemory = ExMemory 267119}
1+
ExBudget {exBudgetCPU = ExCPU 58683921, exBudgetMemory = ExMemory 267719}

0 commit comments

Comments
 (0)