Skip to content

Commit 820d308

Browse files
committed
forking ConcMs
1 parent 00aa2ce commit 820d308

File tree

3 files changed

+22
-23
lines changed

3 files changed

+22
-23
lines changed

src/Command/Commands.hs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,15 @@ import Bot.Random
1414
import Command
1515
import Command.CursedCommand
1616
import Conc
17-
import Control.Monad
18-
import Data.Function ((&))
1917
import Data.List (intercalate)
2018
import Data.Maybe (fromMaybe)
2119
import Data.Monoid ((<>))
22-
import qualified Data.Text as T (pack, unpack, words)
20+
import qualified Data.Text as T (pack, unpack)
2321
import qualified Data.Text.Encoding as TE
2422
import Data.Time.Clock.POSIX
2523
import MessageQueue
2624
import Text.Read (readMaybe)
2725

28-
import Control.Arrow
2926
import Control.Monad.IO.Class
3027
import qualified Data.HashMap.Strict as M
3128

@@ -60,7 +57,7 @@ commandList =
6057
burselfParrotCommand
6158
, Command
6259
"dicegolf"
63-
["!dicegolf"]
60+
["!dicegolf", "!dg"]
6461
(CommandOptions 2 2 True True (const True))
6562
dicegolfCommand
6663
, Command

src/Command/CursedCommand.hs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,12 @@ import Network.HTTP.Types.Status
2222
import MessageQueue
2323
import Data.Text as T
2424
import qualified Data.List as L (head)
25+
import Control.Concurrent
2526

2627
dubiousFact :: ConcM App ()
2728
dubiousFact = do
2829
cont <- taskM $ do
30+
threadDelay 10000000
2931
man <- newManager tlsManagerSettings
3032
let req = "http://opentdb.com/api.php?amount=1&type=boolean"
3133
jsonResult <- httpLbs req man

src/Conc.hs

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,45 +2,45 @@
22
{-# LANGUAGE MultiParamTypeClasses #-}
33
{-# LANGUAGE LambdaCase #-}
44
{-# LANGUAGE ExistentialQuantification #-}
5-
{-# LANGUAGE InstanceSigs #-}
65

76
module Conc where
87

98
import Control.Concurrent
109
import Control.Concurrent.STM
1110
import Control.Monad
1211
import Control.Monad.IO.Class
13-
import qualified Control.Category as C
14-
import Control.Arrow
1512

1613
data ConcM m c
1714
= EndM c
18-
| forall b. IOtaskM (IO b) (b -> ConcM m c)
15+
| forall b. TaskM (IO b) (b -> ConcM m c)
1916
| forall b. PureM (m b) (b -> ConcM m c)
2017

2118
instance (Monad m) => Functor (ConcM m) where
22-
fmap f (EndM c ) = EndM $ f c
23-
fmap f (IOtaskM io cont) = IOtaskM io (fmap f . cont)
24-
fmap f (PureM p cont) = PureM p (fmap f . cont)
19+
fmap f (EndM c ) = EndM $ f c
20+
fmap f (TaskM io cont) = TaskM io (fmap f . cont)
21+
fmap f (PureM p cont) = PureM p (fmap f . cont)
2522

2623
instance (Monad m) => Applicative (ConcM m) where
2724
pure = EndM
28-
t <*> (IOtaskM io cont) = IOtaskM io (\c -> t <*> cont c)
29-
t <*> (PureM p cont) = PureM p (\c -> t <*> cont c)
30-
(EndM f ) <*> (EndM c ) = EndM (f c)
31-
(IOtaskM io cont) <*> (EndM c) = IOtaskM io (\c2 -> cont c2 <*> EndM c)
32-
(PureM p cont) <*> (EndM c ) = PureM p (\c2 -> cont c2 <*> EndM c)
25+
-- t <*> (TaskM io cont) = TaskM io ((t <*>) . cont)
26+
-- t <*> (PureM p cont) = PureM p ((t <*>) . cont)
27+
-- (EndM f ) <*> (EndM c ) = EndM (f c)
28+
-- (TaskM io cont) <*> (EndM c ) = TaskM io ((<*> EndM c) . cont)
29+
-- (PureM p cont) <*> (EndM c ) = PureM p (\c2 -> cont c2 <*> EndM c)
30+
(EndM f ) <*> t = fmap f t
31+
(TaskM io cont) <*> t = TaskM io ((<*> t) . cont)
32+
(PureM p cont) <*> t = PureM p ((<*> t) . cont)
3333

3434
instance (Monad m) => Monad (ConcM m) where
35-
(EndM a ) >>= cont = PureM (pure a) cont
36-
(IOtaskM io cont1) >>= cont2 = IOtaskM io (\a -> cont1 a >>= cont2)
37-
(PureM p cont1) >>= cont2 = PureM p (\a -> cont1 a >>= cont2)
35+
(EndM a ) >>= cont = PureM (pure a) cont
36+
(TaskM io cont1) >>= cont2 = TaskM io ((>>= cont2) . cont1)
37+
(PureM p cont1) >>= cont2 = PureM p ((>>= cont2) . cont1)
3838

3939
pureM :: m c -> ConcM m c
4040
pureM x = PureM x EndM
4141

4242
taskM :: IO c -> ConcM m c
43-
taskM x = IOtaskM x EndM
43+
taskM x = TaskM x EndM
4444

4545
runConcM :: (Monad a, MonadIO a) => [ConcM a ()] -> a ()
4646
runConcM q_ = do
@@ -52,8 +52,8 @@ runConcM q_ = do
5252
case q of
5353
[] -> liftIO $ threadDelay 100000
5454
_ -> forM_ q $ \case
55-
EndM _ -> return ()
56-
IOtaskM io cont -> void . liftIO . forkIO $ do
55+
EndM _ -> return ()
56+
TaskM io cont -> void . liftIO . forkIO $ do
5757
a <- io
5858
atomically $ modifyTVar queue (cont a :)
5959
PureM p cont -> do

0 commit comments

Comments
 (0)