Guest User

Untitled

a guest
Feb 17th, 2019
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.46 KB | None | 0 0
  1. {-# LANGUAGE TypeFamilies #-}
  2. {-# LANGUAGE DeriveGeneric #-}
  3. {-# LANGUAGE TypeOperators #-}
  4. {-# LANGUAGE MultiParamTypeClasses #-}
  5. {-# LANGUAGE DefaultSignatures #-}
  6. {-# LANGUAGE FlexibleContexts #-}
  7.  
  8. import GHC.Generics
  9.  
  10. data Tree a = EmptyTree | Node a (Tree a) (Tree a) deriving (Generic, Show)
  11.  
  12. instance (Hashable a, Show a) => Hashable (Tree a)
  13.  
  14. data HShape =
  15. InnerHash HShape
  16. |Concat [HShape]
  17. |Interleaving [HShape]
  18. |Slice Int Int
  19. |Pad String deriving Show
  20.  
  21. class Hashable a where
  22. toHShape :: a -> HShape
  23. default toHShape :: (Generic a, GHashable (Rep a)) => a -> HShape
  24. toHShape = gtoHShape . from
  25.  
  26. class GHashable f where
  27. gtoHShape :: f a -> HShape
  28.  
  29. instance GHashable U1 where
  30. gtoHShape U1 = Concat []
  31.  
  32. instance (GHashable a, GHashable b) => GHashable (a :*: b) where
  33. gtoHShape (a :*: b) = Concat [InnerHash (gtoHShape a), InnerHash (gtoHShape b)]
  34.  
  35. instance (GHashable a, GHashable b) => GHashable (a :+: b) where
  36. gtoHShape (L1 x) = InnerHash (gtoHShape x)
  37. gtoHShape (R1 x) = InnerHash (gtoHShape x)
  38.  
  39. instance (GHashable a) => GHashable (M1 i c a) where
  40. gtoHShape (M1 x) = Concat [gtoHShape x]
  41.  
  42. instance (Show a) => GHashable (K1 i a) where
  43. gtoHShape (K1 x) = Pad (show x)
  44.  
  45. -- instance GHashable (Tree) where
  46. -- gtoHShape ta = gtoHShape (from ta)
  47.  
  48. a = M1 {unM1 = R1 (M1 {unM1 = M1 {unM1 = K1 {unK1 = 5}} :*: (M1 {unM1 = K1 {unK1 = EmptyTree}} :*: M1 {unM1 = K1 {unK1 = EmptyTree}})})}
  49.  
  50. main = putStr $ show $ from EmptyTree
Add Comment
Please, Sign In to add comment