Last active
April 9, 2019 18:49
-
-
Save ashutosh2411/688f0c9c63f669266aa75468133b8968 to your computer and use it in GitHub Desktop.
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 TypeFamilies #-} | |
{-# LANGUAGE DeriveGeneric #-} | |
{-# LANGUAGE TypeOperators #-} | |
{-# LANGUAGE MultiParamTypeClasses #-} | |
{-# LANGUAGE DefaultSignatures #-} | |
{-# LANGUAGE FlexibleContexts #-} | |
{-# LANGUAGE UndecidableInstances #-} | |
{-# LANGUAGE FlexibleInstances #-} | |
--module DThash where | |
import Data.Data | |
import Data.Word | |
import qualified Codec.Binary.UTF8.String as CodStr | |
import Data.Time.Clock | |
--import Crypto.Hash.SHA256 | |
import Data.Digest.SHA256 | |
--import Data.Digest.SHA384 | |
import System.Environment | |
import GHC.Generics | |
--type BStr = [Word8] | |
--type HashF = [Word8] -> [Word8] | |
type BStr = String | |
type HashF = String -> String | |
--toWord8 :: String -> [Word8] | |
--toWord8 = CodStr.encode | |
toWord8 :: String -> String | |
toWord8 x = x | |
class GHashable f where | |
gcomputeHash :: HashF -> f a -> BStr | |
genericConstrName :: f x -> String | |
instance GHashable U1 where | |
gcomputeHash hashf U1 = hashf (toWord8 "U1") | |
genericConstrName U1 = "" | |
instance (GHashable a, GHashable b) => GHashable (a :*: b) where | |
gcomputeHash hashf (a :*: b) = | |
concat [gcomputeHash hashf a, | |
gcomputeHash hashf b, | |
toWord8 "Pdt1"] | |
genericConstrName (a :*: b) = "" | |
instance (GHashable a, GHashable b) => GHashable (a :+: b) where | |
gcomputeHash hashf (L1 x) = | |
concat [gcomputeHash hashf x, | |
toWord8 "Sum1L1"] | |
gcomputeHash hashf (R1 x) = | |
concat [gcomputeHash hashf x, | |
toWord8 "Sum1R1"] | |
genericConstrName (L1 l) = genericConstrName l | |
genericConstrName (R1 r) = genericConstrName r | |
instance (GHashable a) => GHashable (D1 c a) where | |
gcomputeHash hashf (M1 x) = concat[gcomputeHash hashf x, toWord8 "M1D"] | |
genericConstrName (M1 x) = genericConstrName x | |
instance (GHashable a, Constructor c) => GHashable (C1 c a) where | |
gcomputeHash hashf (M1 x) = concat[gcomputeHash hashf x, hashf $ toWord8 $ genericConstrName (M1 x), toWord8 "M1D"] | |
genericConstrName x = conName x | |
instance (GHashable a) => GHashable (S1 c a) where | |
gcomputeHash hashf (M1 x) = concat[gcomputeHash hashf x, toWord8 "M1S"] | |
genericConstrName x = "" | |
instance (Show a) => GHashable (K1 i a) where | |
gcomputeHash hashf (K1 x) = concat [hashf (toWord8 $ show x), toWord8 "K1"] | |
genericConstrName _ = "" | |
class Hashable a where | |
computeHash :: HashF -> a -> BStr | |
default computeHash :: (Generic a, GHashable (Rep a)) => HashF -> a -> BStr | |
computeHash hashf x = gcomputeHash hashf (from x) | |
data Tree a = EmptyTree | Node a (Tree a) (Tree a) deriving (Generic, Show) | |
instance (Hashable a, Show a) => Hashable (Tree a) | |
instance Hashable Int | |
main = print $ show $ computeHash Prelude.id (Node 5 EmptyTree EmptyTree :: Tree Int) |
Author
ashutosh2411
commented
Apr 9, 2019
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment