Add deriveEq for Plinth similar to deriving stock Eq#7433
Open
Add deriveEq for Plinth similar to deriving stock Eq#7433
Conversation
1bdeaab to
956de09
Compare
Contributor
Execution Budget Golden Diffoutputplutus-benchmark/cardano-loans/test/9.6/main.golden.eval
plutus-benchmark/coop/test/9.6/mustBurnOwnSingleton.golden.eval
plutus-benchmark/linear-vesting/test/9.6/main.golden.eval
plutus-benchmark/script-contexts/test/V3/Data/9.6/purposeIsWellFormed-4.golden.eval
This comment will get updated when changes are made. |
956de09 to
d666dea
Compare
5b30be7 to
5d931a0
Compare
5d931a0 to
396e4e9
Compare
396e4e9 to
97a7b4b
Compare
7593c53 to
3ca2a2e
Compare
SeungheonOh
reviewed
Nov 26, 2025
Collaborator
SeungheonOh
left a comment
There was a problem hiding this comment.
I'm not too confident in my ability to review TH code. So perhaps a look from @Unisay would be nice. But everything looks right from what I can tell.
Collaborator
|
Also, can you check if this works with polymorphic phantom types? I'm curious as per #4537 |
Unisay
reviewed
Dec 1, 2025
Unisay
reviewed
Dec 1, 2025
Unisay
reviewed
Dec 1, 2025
dc7bf5e to
fbb2dbb
Compare
Contributor
If a PR is useful improvement even without its follow-up I'd say yes. |
8042476 to
7557d18
Compare
ana-pantilie
approved these changes
Feb 11, 2026
Contributor
ana-pantilie
left a comment
There was a problem hiding this comment.
Approving because my comments aren't critical, but please take a look at them if you can.
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.
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.
Replace hand-written Eq instances for Rational and These with deriveEq calls. The generated instances are structurally identical to the manual ones.
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).
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.
Variable naming in deriveEq-generated code differs from manual instances, causing golden file updates in plutus-benchmark and plutus-tx-plugin test suites.
Add entries to plutus-tx and plutus-ledger-api changelogs documenting the new deriveEq function and the migration from manual Eq instances.
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.
The golden tests added by deriveEq require the 'diff' binary which is not available in the Windows (mingW64) cross-compilation environment.
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.
Replace commented-out deriveEq code with plain comments explaining why PlutusTx.Eq instances are absent, addressing review feedback.
21ef04e to
144d4f9
Compare
62c8d87 to
cf76559
Compare
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Context
deriveEqfunction to automatically generatePlutusTx.EqinstancesApproach
Implemented a Template Haskell function that generates structural equality similar to GHC's
deriving stock Eq. The implementation:PlutusTx.EqintoPlutusTx.Eq.Class(type class definition) andPlutusTx.Eq.TH(deriving function) for better organizationl1l,r2r) to avoid shadowing warningsPhantomADT eThe generated code handles:
TrueequalityChanges
Template Haskell Implementation
deriveEqfunction usingth-abstractionfor reliable type introspectionApplied Across plutus-ledger-api
Replaced ~100 lines of manual Eq implementations with
deriveEqcalls in:Net result: -101 lines (602 deletions, 501 additions) primarily from removing boilerplate
Tests and Golden Files
Updated Benchmarks