Comments (4)
Test-case:
main :: IO ()
main = do
v <- newTVarIO (7 :: Int)
x <- atomically $ do
stateTVar v (error "strict in f")
writeTVar v 8
readTVar v
print x
If we add bang pattern to stateTVar then the code crashes.
I think we should probably add stateTVar'
as an equivalent of modifyTVar'
. But it's so simple to implement that perhaps it shouldn't even live in the library and we should only fix the incorrect comment.
from stm.
I would argue that the version of stateTVar
that is strict in the tuple would be much more useful than the one that isn't, since it allows you to control what components of the state and result are strict by seq
-ing parts to the tuple, while the lazy version doesn't allow you to control strictness at all. In fact, the strict version is more general than the lazy one, since stateTVarLazy f = stateTVarStrict (\s -> let ~(a, s') = f s in (a, s'))
.
Edit: Actually, they're equally general. stateTVarLazy
can be used to implement stateTVarStrict
like this:
data Box a = Box a
stateTVarStrict f = do
Box a <- stateTVarLazy $ \s -> let !(a, s') = f a in (Box a, s')
return a
But that's inefficient and much more clumsy.
I'm reminded of discussion relating to atomicModifyIORef
, and I think the consensus is that it's problematically lazy exactly because of these reasons, although I can't find any example discussion at the moment.
On an unrelated note, perhaps the elements of the tuple should be swapped to mirror atomicModifyIORef
?
from stm.
Sorry; I wrote the original code in #14 and was less familiar with strictness then, I assumed it was fine as it passed review. FWIW, I did intend the strict version - the docstring explicitly says it's similar to modifyTVar'
the strict form. The code is fairly young so I'd guess not many people are using it yet, and that even less people are depending on the lazy behaviour. So perhaps it's worth just adding the bang in a PR, without defining a second function.
from stm.
On an unrelated note, perhaps the elements of the tuple should be swapped to mirror atomicModifyIORef?
The intention was to mirror state
as in MonadState
, StateT
, etc.
I speculate that the order in atomicModifyIORef
comes from an older convention which was swapped, which you still see in the internal definition of IO/ST and GHC internal code.
from stm.
Related Issues (20)
- Please release the fix to the "flushTBQueue" bug! HOT 4
- Extended Timer API. Feedback requested on a proposal. HOT 3
- If I create a new TChan, what happen if the TChan is not read but the children thread that creates it is closed? HOT 1
- MonadSTM HOT 6
- Please ensure no warnings for incomplete-uni-patterns HOT 1
- TBQueue of size 0 doesn't work as expected HOT 20
- `catchSTM` does not rollback `newTVar` HOT 2
- Add MArray instance for TArray in IO
- newBroadcastTChan question HOT 1
- documentation for "thread blocked indefinitely in an STM transaction"? HOT 1
- registerDelay not implemented on the non-threaded runtime
- Dependency bounds on Hackage are wrong? HOT 9
- TBQueue haddock docs say "blocks" instead of "retries"
- Why is it blocked? HOT 3
- Huge dependency footprint due to `hashable-1.4.0.0` (via `nats`) HOT 4
- Nonexhaustive pattern in `TBQueue`
- add method to snapshot TBQueue without flushing HOT 3
- MonadFail instance (feature request) HOT 2
- Support size for TBQueue HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from stm.