Last active
January 13, 2020 05:16
-
-
Save googleson78/c368cc132bf872181d1e460026d9962b to your computer and use it in GitHub Desktop.
uh..
This file contains 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
{-# LANGUAGE DerivingVia #-} | |
{-# LANGUAGE DerivingStrategies #-} | |
module HashAbuse where | |
import Data.Hashable | |
import Data.Function (on) | |
newtype Vertical = Vertical {getVertical :: (Int, Int)} | |
deriving stock Show | |
deriving Eq via EqOnHash Vertical | |
instance Hashable Vertical where | |
hashWithSalt _ = error "shouldn't be called" | |
hash (Vertical (_, y)) = y | |
newtype RevDiag = RevDiag {getRevDiag :: (Int, Int)} | |
deriving stock Show | |
deriving Eq via EqOnHash RevDiag | |
instance Hashable RevDiag where | |
hashWithSalt _ = error "shouldn't be called" | |
hash (RevDiag (x, y)) = x + y | |
newtype MainDiag = MainDiag {getMainDiag :: (Int, Int)} | |
deriving stock Show | |
deriving Eq via EqOnHash MainDiag | |
instance Hashable MainDiag where | |
hashWithSalt _ = error "shouldn't be called" | |
hash (MainDiag (x, y)) = x - y | |
newtype EqOnHash a = EqOnHash {getEqOnHash :: a} | |
instance Hashable a => Eq (EqOnHash a) where | |
(==) = (==) `on` (hash . getEqOnHash) |
@Icelandjack
Yes! Nearly every time I use DerivingVia
I want this same thing myself.
(btw, do you have an automatic magic detector for DerivingVia
usage? 😅)
(also huge thanks for the extension!)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Once we get dependent types we could parameterise
EqOnHash
on the function and just naming itIf I got that right we will be able to write