Skip to content

Stateful generator application is sometimes too strict #87

@treeowl

Description

@treeowl

We certainly want generator application to modify the generator strictly, but it's not always reasonable to produce the result strictly. I think applyRandomGenM itself probably shouldn't force the result, but there should be a strict function based on it for implementing, e.g., uniformWord32R. For most RandomGenM instances, the change is trivial. For AtomicGenM, we'd need a half-strict version of atomicModifyIORef. For all vaguely recent GHC versions, it would look like this:

import GHC.IORef (atomicModifyIORef2)

atomicModifyIORefHS :: IORef a -> (a -> (a,b)) -> IO b
atomicModifyIORefHS ref f = do
  (_old, (_new, res)) <- atomicModifyIORef2 ref $
    \old -> case f old of
       r@(!_new, _res) -> r
  pure res

It's also possible to implement for older versions, but a bit less efficiently.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions