Skip to content

Add global region and MonadST #26

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

Merged
merged 1 commit into from
Feb 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
"dependencies": {
"purescript-prelude": "^4.0.0",
"purescript-tailrec": "^4.0.0",
"purescript-partial": "^2.0.0"
"purescript-partial": "^2.0.0",
"purescript-unsafe-coerce": "^4.0.0"
},
"devDependencies": {
"purescript-console": "^4.0.0"
Expand Down
17 changes: 17 additions & 0 deletions src/Control/Monad/ST/Class.purs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module Control.Monad.ST.Class where

import Prelude

import Control.Monad.ST (ST)
import Control.Monad.ST.Global (Global)
import Control.Monad.ST.Global as Global
import Effect (Effect)

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

instance monadSTEffect :: MonadST Global Effect where
liftST = Global.toEffect

instance monadSTST :: MonadST s (ST s) where
liftST = identity
18 changes: 18 additions & 0 deletions src/Control/Monad/ST/Global.purs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
module Control.Monad.ST.Global
( Global
, toEffect
) where

import Prelude

import Control.Monad.ST (ST, kind Region)
import Effect (Effect)
import Unsafe.Coerce (unsafeCoerce)

-- | This region allows `ST` computations to be converted into `Effect`
-- | computations so they can be run in a global context.
foreign import data Global :: Region

-- | Converts an `ST` computation into an `Effect` computation.
toEffect :: ST Global ~> Effect
toEffect = unsafeCoerce