Skip to content

Add TH derivings for Plinth's Eq #7430

@bezirg

Description

@bezirg

Problem

Writing PlutusTx.Eq instances by hand is tedious and error-prone. Unlike Haskell's deriving stock Eq, there's no automatic way to derive equality for Plutus types.

Example of current manual approach:

instance PlutusTx.Eq MyType where
  {-# INLINEABLE (==) #-}
  (Con1 a1 b1) == (Con1 a2 b2) = a1 == a2 && b1 == b2
  (Con2 x1) == (Con2 x2) = x1 == x2
  _ == _ = False

This pattern repeats across plutus-ledger-api and user code, adding boilerplate and potential for bugs.

Solution

Add a Template Haskell function to derive PlutusTx.Eq instances:

data MyType = Con1 Integer Bool | Con2 ByteString
PlutusTx.deriveEq ''MyType

The generated code should:

  • Use structural equality matching GHC's stock deriving behavior
  • Include INLINEABLE pragmas for on-chain optimization
  • Support datatypes, newtypes, records, and phantom type parameters

Definition of Done

  • PlutusTx.deriveEq function implemented and exported
  • Tests covering: multi-constructor ADTs, newtypes, records, phantom types
  • Manual Eq instances in plutus-ledger-api replaced with derived versions
  • Documentation with usage examples
  • Changelog entry added

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions