Skip to content

Commit ed36538

Browse files
authored
Merge pull request #125 from andrewthad/fix_example_instances
Fix example instances
2 parents dfebc76 + d6ec159 commit ed36538

File tree

2 files changed

+26
-3
lines changed

2 files changed

+26
-3
lines changed

Data/Hashable/Lifted.hs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,18 +43,18 @@ import Data.Hashable.Class
4343
-- > instance (Hashable w, Hashable1 m) => Hashable1 (WriterT w m) where
4444
-- > liftHashWithSalt h s (WriterT m) =
4545
-- > liftHashWithSalt (liftHashWithSalt2 h hashWithSalt) s m
46-
-- > instance (Hashable1 f, Functor f) => Hashable1 (Free f) where
46+
-- > instance Hashable1 f => Hashable1 (Free f) where
4747
-- > liftHashWithSalt h = go where
4848
-- > go s x = case x of
4949
-- > Pure a -> h s a
50-
-- > Free p -> liftHashWithSalt go p
50+
-- > Free p -> liftHashWithSalt go s p
5151
--
5252
-- The 'Hashable' instances for these types can be trivially recovered with
5353
-- 'hashWithSalt1':
5454
--
5555
-- > instance (Hashable w, Hashable1 m, Hashable a) => Hashable (WriterT w m a) where
5656
-- > hashWithSalt = hashWithSalt1
57-
-- > instance (Hashable1 f, Hashable a) => Hashable1 (Free f a) where
57+
-- > instance (Hashable1 f, Hashable a) => Hashable (Free f a) where
5858
-- > hashWithSalt = hashWithSalt1
5959

6060
--

examples/Main.hs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{-# LANGUAGE DeriveGeneric #-}
22
import Data.Hashable
3+
import Data.Hashable.Lifted
34
import GHC.Generics (Generic)
45

56
data Foo
@@ -25,3 +26,25 @@ main = do
2526
print . hash $ Foo2 "hello" ()
2627
putStrLn "Hashing Bar"
2728
print . hash $ Bar 55.50 9.125
29+
30+
-----------------------------------
31+
-- Higher Rank Hashable Examples --
32+
-----------------------------------
33+
34+
newtype WriterT w m a = WriterT { runWriterT :: m (a, w) }
35+
data Free f a = Pure a | Free (f (Free f a))
36+
37+
instance (Hashable w, Hashable1 m) => Hashable1 (WriterT w m) where
38+
liftHashWithSalt h s (WriterT m) =
39+
liftHashWithSalt (liftHashWithSalt2 h hashWithSalt) s m
40+
instance Hashable1 f => Hashable1 (Free f) where
41+
liftHashWithSalt h = go where
42+
go s x = case x of
43+
Pure a -> h s a
44+
Free p -> liftHashWithSalt go s p
45+
46+
instance (Hashable w, Hashable1 m, Hashable a) => Hashable (WriterT w m a) where
47+
hashWithSalt = hashWithSalt1
48+
instance (Hashable1 f, Hashable a) => Hashable (Free f a) where
49+
hashWithSalt = hashWithSalt1
50+

0 commit comments

Comments
 (0)