File tree Expand file tree Collapse file tree 3 files changed +14
-7
lines changed
Expand file tree Collapse file tree 3 files changed +14
-7
lines changed Original file line number Diff line number Diff line change @@ -6,19 +6,16 @@ module PlutusTx.Eq.Class
66import PlutusTx.Bool
77import PlutusTx.Builtins qualified as Builtins
88
9- {- HLINT ignore -}
10-
119infix 4 ==
1210
13- -- Copied from the GHC definition
11+ {- | The 'Eq' class defines equality ('==').
1412
15- -- | The 'Eq' class defines equality ('==').
13+ (/=) deliberately omitted, to make this a one-method class which has a
14+ simpler representation
15+ -}
1616class Eq a where
1717 (==) :: a -> a -> Bool
1818
19- -- (/=) deliberately omitted, to make this a one-method class which has a
20- -- simpler representation
21-
2219infix 4 /=
2320(/=) :: (Eq a ) => a -> a -> Bool
2421x /= y = not (x == y)
Original file line number Diff line number Diff line change @@ -10,6 +10,10 @@ import Language.Haskell.TH as TH
1010import Language.Haskell.TH.Datatype as TH
1111import Data.Deriving.Internal (varTToName )
1212
13+ {- | derive a PlutusTx.Eq instance for a datatype/newtype, similar to Haskell's `deriving stock Eq`.
14+
15+ One shortcoming compared to Haskell's deriving is that you cannot `PlutusTx.deriveEq` for polymorphic phantom types.
16+ -}
1317deriveEq :: TH. Name -> TH. Q [TH. Dec ]
1418deriveEq name = do
1519 TH. DatatypeInfo
Original file line number Diff line number Diff line change @@ -23,6 +23,10 @@ data SomeLargeADT a b c d e =
2323 deriving stock HS.Eq
2424deriveEq ''SomeLargeADT
2525
26+ newtype PhantomADT e = PhantomADT ()
27+ deriving stock HS.Eq
28+ deriveEq ''PhantomADT
29+
2630eqTests :: TestTree
2731eqTests =
2832 let v1 :: SomeLargeADT () BuiltinString () () () = SomeLargeADT1 1 () Tx. True " foobar" () ()
@@ -36,6 +40,8 @@ eqTests =
3640 [testCase " reflexive1" $ (v1 Tx. == v1) @?= (v1 HS. == v1)
3741 , testCase " reflexive2" $ (v2 Tx. == v2) @?= (v2 HS. == v2)
3842 , testCase " reflexive3" $ (v3 Tx. == v3) @?= (v3 HS. == v3)
43+ -- currently does not work with polymorphic phantom types, remove the type annotation when support is added
44+ , testCase " phantom" $ (PhantomADT @ () () Tx. == PhantomADT () ) @?= (PhantomADT () HS. == PhantomADT () )
3945 , testCase " shortcircuit" $ (v3 Tx. == v3Error1) @?= (v3 Tx. == v3Error1) -- should not throw an error
4046 , testCase " throws" $ try @ SomeException (evaluate $ v3 Tx. == v3Error2) >>= assertBool " did not throw error" . isLeft -- should throw erro
4147 ]
You can’t perform that action at this time.
0 commit comments