Skip to content

Commit 645b3d5

Browse files
committed
Update example
1 parent 064eb37 commit 645b3d5

File tree

4 files changed

+53
-44
lines changed

4 files changed

+53
-44
lines changed

README.md

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,16 @@ compiler architectures.
99
# Example
1010

1111
```haskell
12+
{-# language FlexibleInstances #-}
1213
{-# language GADTs #-}
13-
{-# language NoImplicitPrelude #-}
14-
{-# language OverloadedStrings #-}
1514
{-# language StandaloneDeriving #-}
1615
{-# language TemplateHaskell #-}
1716

18-
import Protolude
19-
20-
import Data.GADT.Compare.TH (deriveGEq, deriveGCompare)
17+
import Control.Monad.IO.Class
18+
import Data.GADT.Compare.TH (deriveGEq)
19+
import Data.Hashable
20+
import Data.Some
21+
import Data.IORef
2122
import qualified Rock
2223

2324
data Query a where
@@ -27,13 +28,22 @@ data Query a where
2728
D :: Query Integer
2829

2930
deriving instance Show (Query a)
30-
3131
deriveGEq ''Query
32-
deriveGCompare ''Query
32+
33+
instance Hashable (Query a) where
34+
hashWithSalt salt query =
35+
case query of
36+
A -> hashWithSalt salt (0 :: Int)
37+
B -> hashWithSalt salt (1 :: Int)
38+
C -> hashWithSalt salt (2 :: Int)
39+
D -> hashWithSalt salt (3 :: Int)
40+
41+
instance Hashable (Some Query) where
42+
hashWithSalt salt (Some query) = hashWithSalt salt query
3343

3444
rules :: Rock.Rules Query
3545
rules key = do
36-
putText $ "Fetching " <> show key
46+
liftIO $ putStrLn $ "Fetching " <> show key
3747
case key of
3848
A -> pure 10
3949
B -> do
@@ -48,17 +58,14 @@ rules key = do
4858
main :: IO ()
4959
main = do
5060
do
51-
putText "Running"
61+
liftIO $ putStrLn "Running"
5262
result <- Rock.runTask rules (Rock.fetch D)
5363
print result
5464
do
55-
putText "Running with memoisation"
56-
memoVar <- newMVar mempty
57-
result <-
58-
Rock.runTask
59-
(Rock.memoise memoVar rules)
60-
(Rock.fetch D)
61-
print result
65+
liftIO $ putStrLn "Running with memoisation"
66+
memoVar <- newIORef mempty
67+
result <- Rock.runTask (Rock.memoise memoVar rules) (Rock.fetch D)
68+
liftIO $ print result
6269
```
6370

6471
Prints

examples/Spreadsheet.hs

Lines changed: 24 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1+
{-# language FlexibleInstances #-}
12
{-# language GADTs #-}
2-
{-# language NoImplicitPrelude #-}
3-
{-# language OverloadedStrings #-}
43
{-# language StandaloneDeriving #-}
54
{-# language TemplateHaskell #-}
65

7-
import Protolude
8-
9-
import Data.GADT.Compare.TH (deriveGEq, deriveGCompare)
6+
import Control.Monad.IO.Class
7+
import Data.GADT.Compare.TH (deriveGEq)
8+
import Data.Hashable
9+
import Data.Some
10+
import Data.IORef
1011
import qualified Rock
1112

1213
data Query a where
@@ -16,13 +17,22 @@ data Query a where
1617
D :: Query Integer
1718

1819
deriving instance Show (Query a)
19-
2020
deriveGEq ''Query
21-
deriveGCompare ''Query
21+
22+
instance Hashable (Query a) where
23+
hashWithSalt salt query =
24+
case query of
25+
A -> hashWithSalt salt (0 :: Int)
26+
B -> hashWithSalt salt (1 :: Int)
27+
C -> hashWithSalt salt (2 :: Int)
28+
D -> hashWithSalt salt (3 :: Int)
29+
30+
instance Hashable (Some Query) where
31+
hashWithSalt salt (Some query) = hashWithSalt salt query
2232

2333
rules :: Rock.Rules Query
2434
rules key = do
25-
putText $ "Fetching " <> show key
35+
liftIO $ putStrLn $ "Fetching " <> show key
2636
case key of
2737
A -> pure 10
2838
B -> do
@@ -37,24 +47,11 @@ rules key = do
3747
main :: IO ()
3848
main = do
3949
do
40-
putText "Running"
41-
result <- Rock.runTask Rock.sequentially rules (Rock.fetch D)
50+
liftIO $ putStrLn "Running"
51+
result <- Rock.runTask rules (Rock.fetch D)
4252
print result
4353
do
44-
putText "Running with memoisation"
45-
memoVar <- newMVar mempty
46-
result <-
47-
Rock.runTask
48-
Rock.sequentially
49-
(Rock.memoise memoVar rules)
50-
(Rock.fetch D)
51-
print result
52-
do
53-
putText "Running with memoisation using the parallel strategy"
54-
memoVar <- newMVar mempty
55-
result <-
56-
Rock.runTask
57-
Rock.inParallel
58-
(Rock.memoise memoVar rules)
59-
(Rock.fetch D)
60-
print result
54+
liftIO $ putStrLn "Running with memoisation"
55+
memoVar <- newIORef mempty
56+
result <- Rock.runTask (Rock.memoise memoVar rules) (Rock.fetch D)
57+
liftIO $ print result

rock.cabal

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,11 @@ executable rock-spreadsheet
7070
-threaded
7171
hs-source-dirs: examples
7272
default-language: Haskell2010
73-
build-depends: base, rock, protolude, dependent-sum-template
73+
build-depends: base
74+
, dependent-sum
75+
, dependent-sum-template
76+
, hashable
77+
, rock
7478

7579
test-suite test-rock
7680
type: exitcode-stdio-1.0

stack.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ extra-deps:
55
- dependent-hashmap-0.1.0.0
66
- constraints-extras-0.3.0.2
77
- dependent-sum-0.6.2.0
8+
- dependent-sum-template-0.1.0.3

0 commit comments

Comments
 (0)