Skip to content

Global region, take two #19

Closed
Closed
@no-longer-on-githu-b

Description

@no-longer-on-githu-b

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.

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