Skip to content

Global region, take two #19

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
no-longer-on-githu-b opened this issue Apr 30, 2018 · 3 comments · Fixed by #26
Closed

Global region, take two #19

no-longer-on-githu-b opened this issue Apr 30, 2018 · 3 comments · Fixed by #26

Comments

@no-longer-on-githu-b
Copy link

no-longer-on-githu-b commented Apr 30, 2018

Previous issue was #7, but that was still about effect rows.

Now that we no longer use effect rows, it is no longer possible to use ST globally by adding st :: ST a to the effect row of main.

I think that we can abstract over Effect and ST so that code that uses mutation can be used on both global and local state, as follows.

foreign import data Global :: Region

effectToST :: Effect ~> ST Global
stToEffect :: ST Global ~> Effect

instance MonadEffect (ST Global) where
  liftEffect = effectToST

Now we can abstract over it.

class MonadST s m | m -> s where
  liftST :: ST s ~> m

instance monadSTEffect :: MonadST Global Effect where
  liftST = stToEffect

instance monadSTST :: MonadST s ST where
  liftST = id

Now, STRef and Ref can be unified, allowing for code reuse. Similar for STArray and something I don't think exists yet (useful for fixing purescript-node/purescript-node-process#8).

foreign import data Ref :: Region -> Type -> Type

newRef :: forall s m a. MonadST s m => a -> m (Ref s a)
readRef :: forall s m a. MonadST s m => Ref s a -> m a
writeRef :: forall s m a. MonadST s m => Ref s a -> a -> m Unit

Additionally, ST can be used without explicit lifting with monad transformers.

@garyb
Copy link
Member

garyb commented Apr 30, 2018

Moving everything to use MonadST constraints would destroy the ability to optimize ST I think (not that it is optimized in the 0.12 RC just now).

Honest question: why/when would you want to do this? I can't think of any situations where I've wanted a non-closed ST.

@no-longer-on-githu-b
Copy link
Author

no-longer-on-githu-b commented Apr 30, 2018

When you have a library that already uses ST, but you want to use it from Effect (for example STArray; I've been using MVector lately in Haskell in both IO and ST, for production and testing respectively).

Granted, explicit liftST is enough for that, you don't need the constraint for all the operations (it would just be more convenient).

@eric-corumdigital
Copy link

Am wanting to use STObject in Effect. Got my upvote.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants