Skip to content
This repository was archived by the owner on Jan 17, 2020. It is now read-only.

Commit 72e082e

Browse files
committed
first commit
0 parents  commit 72e082e

File tree

5 files changed

+133
-0
lines changed

5 files changed

+133
-0
lines changed

.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/bower_components/
2+
/node_modules/
3+
/.pulp-cache/
4+
/output/
5+
/.psci*
6+
/src/.webpack.js

bower.json

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"name": "purescript-io",
3+
"ignore": [
4+
"**/.*",
5+
"node_modules",
6+
"bower_components",
7+
"output"
8+
],
9+
"dependencies": {
10+
"purescript-aff": "1.1.0"
11+
},
12+
"devDependencies": {
13+
"purescript-psci-support": "^1.0.0"
14+
}
15+
}

src/Control/Monad/IO.purs

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
module Control.Monad.IO (IO, INFINITY, AffIO(..), runIO) where
2+
import Prelude
3+
4+
import Control.Alt (class Alt, alt)
5+
import Control.Alternative (class Alternative)
6+
import Control.Monad.Eff (Eff)
7+
import Control.Monad.Eff.Class (class MonadEff, liftEff)
8+
import Control.Monad.Aff (Aff)
9+
import Control.Monad.Aff.Class (class MonadAff)
10+
import Control.Monad.Eff.Exception (Error)
11+
import Control.Monad.Error.Class (class MonadError, throwError, catchError)
12+
import Control.Monad.Rec.Class (class MonadRec, tailRecM)
13+
import Control.MonadPlus (class MonadZero, class MonadPlus, empty)
14+
import Control.Parallel.Class (class MonadRace, class MonadPar, par, race, stall)
15+
import Control.Plus (class Plus)
16+
17+
import Data.Monoid (class Monoid, mempty)
18+
19+
import Unsafe.Coerce (unsafeCoerce)
20+
21+
foreign import data IO :: * -> *
22+
23+
foreign import data INFINITY :: !
24+
25+
type AffIO a = Aff (infinity :: INFINITY) a
26+
27+
runIO :: forall a. IO a -> AffIO a
28+
runIO = unsafeCoerce
29+
30+
toIO :: forall e a. Aff e a -> IO a
31+
toIO = unsafeCoerce
32+
33+
instance semigroupIO :: (Semigroup a) => Semigroup (IO a) where
34+
append a b = toIO (append (runIO a) (runIO b))
35+
36+
instance monoidIO :: (Monoid a) => Monoid (IO a) where
37+
mempty = toIO (pure mempty)
38+
39+
instance functorIO :: Functor IO where
40+
map f fa = toIO (map f (runIO fa))
41+
42+
instance applyIO :: Apply IO where
43+
apply ff fa = toIO (apply (runIO ff) (runIO fa))
44+
45+
instance applicativeIO :: Applicative IO where
46+
pure v = toIO (pure v)
47+
48+
instance bindIO :: Bind IO where
49+
bind fa f = toIO (bind (runIO fa) (unsafeCoerce f))
50+
51+
instance monadIO :: Monad IO
52+
53+
instance monadEffIO :: MonadEff e IO where
54+
liftEff = liftEff'
55+
where
56+
liftEff' :: forall a. Eff e a -> IO a
57+
liftEff' eff = toIO (liftEff eff :: Aff e a)
58+
59+
instance monadAffIO :: MonadAff e IO where
60+
liftAff = toIO
61+
62+
instance monadErrorIO :: MonadError Error IO where
63+
throwError e = toIO (throwError e)
64+
65+
catchError io f = toIO (catchError (runIO io) (runIO <$> f))
66+
67+
instance altIO :: Alt IO where
68+
alt a1 a2 = toIO (alt (runIO a1) (runIO a2))
69+
70+
instance plusIO :: Plus IO where
71+
empty = toIO empty
72+
73+
instance alternativeIO :: Alternative IO
74+
75+
instance monadZero :: MonadZero IO
76+
77+
instance monadPlusIO :: MonadPlus IO
78+
79+
instance monadRecIO :: MonadRec IO where
80+
tailRecM f a = toIO (tailRecM (unsafeCoerce f) a)
81+
82+
instance monadParIO :: MonadPar IO where
83+
par f ma mb = toIO (par f (runIO ma) (runIO mb))
84+
85+
instance monadRaceIO :: MonadRace IO where
86+
stall = toIO stall
87+
race a1 a2 = toIO (race (runIO a1) (runIO a2))

src/Control/Monad/IO/Class.purs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
module Control.Monad.IO.Class where
2+
import Control.Category (id)
3+
import Control.Monad (class Monad)
4+
import Control.Monad.Aff (Aff)
5+
import Control.Monad.IO (IO)
6+
7+
import Unsafe.Coerce (unsafeCoerce)
8+
9+
class Monad m <= MonadIO m where
10+
liftIO :: forall a. IO a -> m a
11+
12+
instance monadIOIO :: MonadIO IO where
13+
liftIO = id
14+
15+
instance monadIOAff :: MonadIO (Aff e) where
16+
liftIO = unsafeCoerce

src/Main.purs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
module Main where
2+
3+
import Prelude
4+
import Control.Monad.Eff (Eff)
5+
import Control.Monad.Eff.Console (CONSOLE, log)
6+
7+
main :: forall e. Eff (console :: CONSOLE | e) Unit
8+
main = do
9+
log "Hello sailor!"

0 commit comments

Comments
 (0)