TristanCacqueray / ki-effectful

MIT License
7 stars 1 forks source link

Should `atomically` be in `StructuredConcurrency`? #9

Open michaelpj opened 3 months ago

michaelpj commented 3 months ago

It seems too powerful to me. STM is great and all, but I would expect StructuredConcurrency to just be the ki stuff.

The alternative would be to just have await and awaitAll in Eff rather than STM.

TristanCacqueray commented 3 months ago

That's a good point, though it might be necessary to call await within an existing transaction (see https://github.com/awkward-squad/ki/discussions/12#discussioncomment-3394438).

I included atomically to replicate the Concurrent effect from effectful, which covers both Control.Concurrent and Control.Concurrent.STM.

michaelpj commented 3 months ago

Okay, I can see the reasons for having the STM version. I guess I might have thought that if you wanted to compose it with other STM stuff you would just incur a Concurrent constraint. I guess this is a bigger design question about how finely to slice these effects. I kind of wish effectful exposed an STM effect that just let you do atomically, that would actually be quite nice. Then you could imagine

await :: (StructuredConcurrency :> es) => ThreadId -> Eff es ()
-- you're going to need `STM :> es` to get rid of this
awaitSTM :: ThreadId -> STM ()

I don't have a good answer here, just thinking around.

TristanCacqueray commented 3 months ago

Thank you for sharing your thought @michaelpj , I appreciate the suggestions. If effectful exposed an STM effect, then I would not include atomically.

Though I'm not sure what kind of power would warrant such a fine grained effect, and since atomically is such a good match for StructuredConcurrency, then I think it makes sense to provide it out of the box.

michaelpj commented 3 months ago

Right, there's definitely a principled position which is something like "StructuredConcurrency aims to allow 'well-behaved' concurrency; STM is 'well-behaved'; so it includes STM". The alternative would be that StructuredConcurrency is really just the scoped forking.

I guess the way I've been thinking of it is that subset-of-IO effects are nice in that they let you say that some stuff is not happening. STM is pretty benign so it's not that useful to say that scoped forking is happening but not STM. On the other hand, maybe I would like to say that some code is only using STM and not doing any forking at all... Sounds like an issue for effectful, though!