module Data.Hashable.Class( Hashable(..), Hashable1(..), Hashable2(..), hashWithSalt1, hashWithSalt2, defaultLiftHashWithSalt, ) whereimport Data.Functor.Classesimport Data.Functor.Constimport Data.Hashableimport Data.Listimport qualified Data.List.NonEmpty as NEimport Data.Proxyclass (Eq1 t {-, forall a. Hashable a => Hashable (t a)-}) => Hashable1 t where liftHashWithSalt :: (Int -> a -> Int) -> Int -> t a -> Intclass (Eq2 t {-, forall a. Hashable a => Hashable1 (t a)-}) => Hashable2 t where liftHashWithSalt2 :: (Int -> a -> Int) -> (Int -> b -> Int) -> Int -> t a b -> InthashWithSalt1 :: (Hashable1 f, Hashable a) => Int -> f a -> InthashWithSalt1 = liftHashWithSalt hashWithSalthashWithSalt2 :: (Hashable2 f, Hashable a, Hashable b) => Int -> f a b -> InthashWithSalt2 = liftHashWithSalt2 hashWithSalt hashWithSaltdefaultLiftHashWithSalt :: (Hashable2 f, Hashable a) => (Int -> b -> Int) -> Int -> f a b -> IntdefaultLiftHashWithSalt h = liftHashWithSalt2 hashWithSalt hhashUsing :: (Hashable b) => (a -> b) -- ^ Transformation function. -> Int -- ^ Salt. -> a -- ^ Value to transform. -> InthashUsing f salt x = hashWithSalt salt (f x)instance Hashable1 Maybe where liftHashWithSalt _ s Nothing = s `hashInt` 0 liftHashWithSalt h s (Just a) = s `hashInt` distinguisher `h` ainstance Hashable a => Hashable1 (Either a) where liftHashWithSalt = defaultLiftHashWithSaltinstance Hashable2 Either where liftHashWithSalt2 h _ s (Left a) = s `hashInt` 0 `h` a liftHashWithSalt2 _ h s (Right b) = s `hashInt` distinguisher `h` binstance Hashable a1 => Hashable1 ((,) a1) where liftHashWithSalt = defaultLiftHashWithSaltinstance Hashable2 (,) where liftHashWithSalt2 h1 h2 s (a1, a2) = s `h1` a1 `h2` a2instance Hashable1 [] where liftHashWithSalt h salt arr = finalise (foldl' step (salt, 0) arr) where finalise (s, l) = hashWithSalt s l step (s, l) x = (h s x, l + 1)instance Hashable2 Const where liftHashWithSalt2 f _ salt (Const x) = f salt xinstance Hashable1 Proxy where liftHashWithSalt _ s _ = sinstance Hashable1 NE.NonEmpty where liftHashWithSalt h salt (a NE.:| as) = liftHashWithSalt h (h salt a) as