|
1 | | -module Control.Monad.IO (IO, INFINITY, AffIO(..), runIO) where |
2 | | - import Prelude |
| 1 | +module Control.Monad.IO |
| 2 | + ( module Control.Monad.IO.Effect |
| 3 | + , IO(..) |
| 4 | + , runIO |
| 5 | + , runIO' |
| 6 | + , launchIO |
| 7 | + ) where |
3 | 8 |
|
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) |
| 9 | +import Control.Alt (class Alt) |
| 10 | +import Control.Alternative (class Alternative) |
| 11 | +import Control.Monad.Aff (Aff, launchAff) |
| 12 | +import Control.Monad.Aff.Class (class MonadAff) |
| 13 | +import Control.Monad.Aff.Unsafe (unsafeCoerceAff) |
| 14 | +import Control.Monad.Eff.Class (class MonadEff, liftEff) |
| 15 | +import Control.Monad.Eff.Exception (Error) |
| 16 | +import Control.Monad.Eff.Unsafe (unsafeCoerceEff) |
| 17 | +import Control.Monad.Error.Class (class MonadError) |
| 18 | +import Control.Monad.IO.Effect (INFINITY) |
| 19 | +import Control.Monad.IOSync (IOSync) |
| 20 | +import Control.Monad.Rec.Class (class MonadRec) |
| 21 | +import Control.MonadZero (class MonadZero) |
| 22 | +import Control.Plus (class Plus) |
| 23 | +import Data.Monoid (class Monoid) |
| 24 | +import Data.Newtype (class Newtype, unwrap, wrap) |
| 25 | +import Prelude |
16 | 26 |
|
17 | | - import Data.Monoid (class Monoid, mempty) |
| 27 | +newtype IO a = IO (Aff (infinity :: INFINITY) a) |
18 | 28 |
|
19 | | - import Unsafe.Coerce (unsafeCoerce) |
| 29 | +runIO :: IO ~> Aff (infinity :: INFINITY) |
| 30 | +runIO = unwrap |
20 | 31 |
|
21 | | - foreign import data IO :: * -> * |
| 32 | +runIO' :: ∀ eff. IO ~> Aff (infinity :: INFINITY | eff) |
| 33 | +runIO' = unsafeCoerceAff <<< unwrap |
22 | 34 |
|
23 | | - foreign import data INFINITY :: ! |
| 35 | +launchIO :: ∀ a. IO a -> IOSync Unit |
| 36 | +launchIO = void <<< liftEff <<< launchAff <<< unwrap |
24 | 37 |
|
25 | | - type AffIO a = Aff (infinity :: INFINITY) a |
| 38 | +derive instance newtypeIO :: Newtype (IO a) _ |
26 | 39 |
|
27 | | - runIO :: forall a. IO a -> AffIO a |
28 | | - runIO = unsafeCoerce |
| 40 | +derive newtype instance functorIO :: Functor IO |
| 41 | +derive newtype instance applyIO :: Apply IO |
| 42 | +derive newtype instance applicativeIO :: Applicative IO |
| 43 | +derive newtype instance bindIO :: Bind IO |
| 44 | +derive newtype instance monadIO :: Monad IO |
29 | 45 |
|
30 | | - toIO :: forall e a. Aff e a -> IO a |
31 | | - toIO = unsafeCoerce |
| 46 | +derive newtype instance monadRecIO :: MonadRec IO |
32 | 47 |
|
33 | | - instance semigroupIO :: (Semigroup a) => Semigroup (IO a) where |
34 | | - append a b = toIO (append (runIO a) (runIO b)) |
| 48 | +derive newtype instance semigroupIO :: (Semigroup a) => Semigroup (IO a) |
35 | 49 |
|
36 | | - instance monoidIO :: (Monoid a) => Monoid (IO a) where |
37 | | - mempty = toIO (pure mempty) |
| 50 | +derive newtype instance monoidIO :: (Monoid a) => Monoid (IO a) |
38 | 51 |
|
39 | | - instance functorIO :: Functor IO where |
40 | | - map f fa = toIO (map f (runIO fa)) |
| 52 | +instance monadAffIO :: MonadAff eff IO where |
| 53 | + liftAff = wrap <<< unsafeCoerceAff |
41 | 54 |
|
42 | | - instance applyIO :: Apply IO where |
43 | | - apply ff fa = toIO (apply (runIO ff) (runIO fa)) |
| 55 | +instance monadEffIO :: MonadEff eff IO where |
| 56 | + liftEff = wrap <<< liftEff <<< unsafeCoerceEff |
44 | 57 |
|
45 | | - instance applicativeIO :: Applicative IO where |
46 | | - pure v = toIO (pure v) |
| 58 | +derive newtype instance monadErrorIO :: MonadError Error IO |
47 | 59 |
|
48 | | - instance bindIO :: Bind IO where |
49 | | - bind fa f = toIO (bind (runIO fa) (unsafeCoerce f)) |
| 60 | +derive newtype instance altIO :: Alt IO |
50 | 61 |
|
51 | | - instance monadIO :: Monad IO |
| 62 | +derive newtype instance plusIO :: Plus IO |
52 | 63 |
|
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) |
| 64 | +derive newtype instance alternativeIO :: Alternative IO |
58 | 65 |
|
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)) |
| 66 | +derive newtype instance monadZeroIO :: MonadZero IO |
0 commit comments