-
Notifications
You must be signed in to change notification settings - Fork 0
/
RefCache.hs
229 lines (164 loc) · 5.27 KB
/
RefCache.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
{-# LANGUAGE AutoDeriveTypeable, LambdaCase, RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables, TupleSections, TypeFamilies #-}
{- |
usage:
@
import qualified Data.RefCache as RefCache
import Data.RefCache (RefCache)
@
-}
module Data.RefCache
( RefCache
, empty
, newCache
, insert
, lookup
, cacheByRef
, traverseShared
, cacheIOByRef
, traverseSharedIO
, cacheSTByRef
, traverseSharedST
) where
import Control.Exception (evaluate)
import Control.Monad
import Control.Monad.ST
import Control.Monad.ST.Unsafe
import Data.IntMap (IntMap)
import qualified Data.IntMap as IntMap
import Data.IORef
import qualified Data.List as List
import Prelude hiding (lookup)
import System.Mem.StableName
{- |
the key type @k@ and the value type @v@ have a functional dependency on an "item type" @i@. this avoids inserting differently-type values into the same key, which would segfault from unsafeCoerce.
the value type @v@ has a functional dependency on the key type @k@.
is abstract.
-}
newtype RefCache k v = RefCache (IntMap [(StableName k, IORef v)])
-- TODO type role nominal RefCache
{- |
-}
empty :: RefCache k v
empty = RefCache IntMap.empty
newCache :: IO (IORef (RefCache k v))
newCache = newIORef empty
insert
:: k -- ^ strict in the key
-> v
-> RefCache k v
-> IO (StableName k, RefCache k v)
insert x y m = do
k <- forceStableName x
v <- newIORef y
return (k, insertRef k v m)
{- |
-}
insertRef :: StableName k -> IORef v -> RefCache k v -> RefCache k v
insertRef k v (RefCache m) = RefCache(IntMap.insertWith (++) (hashStableName k) [(k, v)] m)
-- | strict
forceStableName :: a -> IO (StableName a)
forceStableName x = evaluate x >> makeStableName x
lookup
:: k -- ^ strict in the key
-> RefCache k v
-> IO (Maybe v)
lookup x m = do
k <- forceStableName x
let v = lookupRef k m
y <- readIORef `traverse` v
return y
{- |
hashes with 'hashStableName', disambiguate with 'eqStableName' (via 'Eq').
-}
lookupRef :: StableName k -> RefCache k v -> Maybe (IORef v)
lookupRef k (RefCache m) = case (List.lookup (k) <=< IntMap.lookup (hashStableName k)) m of
Nothing -> Nothing
Just v -> Just v
{- |
the cached function becomes strict in @a@.
@(\f -> 'unsafePerformIO' (cacheByRef f)) :: (a -> b) -> (a -> IO b)@ should be safe:
* when there's no let-floating
*
specializations:
@
cacheByRef :: (a -> b -> c) -> IO (a -> IO (b -> c))
cacheByRef :: (forall x. f x -> m (g x)) -> IO (forall x. f x -> IO (m (g x)))
@
(uses an 'IORef'.)
related: Section 3 ("Benign Side Effects") in <http://community.haskell.org/~simonmar/papers/weak.pdf Stretching the Storage Manager: Weak Pointers an Stable Names in Haskell>
-}
cacheByRef :: (a -> b) -> IO (a -> IO b)
cacheByRef f = cacheIOByRef (return.f)
-- cacheByRef f = do
-- c <- newCache
-- return$ \x -> do
-- readIORef c >>= lookup x >>= \case
-- Just y -> do
-- return y
-- Nothing -> do
-- let y = f x
-- k <- forceStableName x
-- v <- newIORef y
-- _ <- atomicModifyIORef' c ((,()) . insertRef k v)
-- return y
-- {-# NOINLINE cacheByRef #-}
{- | like 'traverse', but preserves sharing.
when @m@ is pure (e.g. @'State' Int@) (i.e. can't observe sharing), or more generally the effect @u@ is pure, 'traverseShared' shoud coincide with 'traverse'. when @m@ is impure (e.g. @IO@), the effect @u@ can violate referential transparency.
for example, we can "poke some holes" with @let@, then "fill those holes" with a ref like 'newIORef'.
>>> (traverse readIORef . traverse (atomicModifyIORef (+1)) . traverseShared newIORef) [0,0,0] -- unshared
[1,1,1]
>>> let x = 0
>>> (traverse readIORef . traverse (atomicModifyIORef (+1)) . traverseShared newIORef) [x,x,x] -- shared
[3,3,3]
pure Haskell can't observe the sharing which distinguishes @let x = 0 in [x,x,x]@ from @[0,0,0]@. hence, 'traverseShared' is impure, returning from 'IO'.
recover input with @'Identity'@, like 'traverse's @identity@ law:
>>> getIdentity <$> traverseShared Identity [0,0,0]
[0,0,0]
recover sharing-observing graph with (\_ -> (Const <$> newUniqueReally)):
>>>
@traverseShared 'return'@ preserves sharing:
>>>
to keep the sharing "as you see it" in the source, call GHC with these options:
*
*
*
-}
traverseShared :: (Traversable t) => (a -> b) -> t a -> IO (t b)
traverseShared u t = do
u' <- cacheByRef u
traverse u' t
cacheIOByRef :: (a -> IO b) -> IO (a -> IO b)
cacheIOByRef f = do
c <- newCache
return$ \x -> do
readIORef c >>= lookup x >>= \case
Just y -> do
return y
Nothing -> do
-- print "Miss"
y <- f x
k <- forceStableName x
v <- newIORef y
_ <- atomicModifyIORef' c ((,()) . insertRef k v)
return y
{-# NOINLINE cacheIOByRef #-}
traverseSharedIO
:: forall t a b. (Traversable t)
=> (a -> IO b)
-> t a
-> IO (t b)
traverseSharedIO u t = do
u' <- cacheIOByRef u
traverse u' t
-- doesn't share
-- traverseSharedIO u t = do
-- u' <- cacheByRef u
-- t' <- traverse u' t
-- traverse id t'
cacheSTByRef :: (a -> ST s b) -> IO (a -> IO b)
cacheSTByRef u = cacheIOByRef (unsafeSTToIO.u)
traverseSharedST :: (Traversable t) => (a -> ST s b) -> t a -> IO (t b)
traverseSharedST u t = do
u' <- cacheSTByRef u
traverse u' t