Skip to content

Commit 3a3cf56

Browse files
committed
io-sim-por: introduced InternalTypes module
Share * `ThreadControl` * `ControlStack` between `IOSim` and `IOSimPOR`.
1 parent 32412c3 commit 3a3cf56

File tree

4 files changed

+57
-60
lines changed

4 files changed

+57
-60
lines changed

io-sim/io-sim.cabal

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ library
2929
Control.Monad.IOSim,
3030
Control.Monad.IOSim.Types
3131
other-modules: Control.Monad.IOSim.Internal,
32+
Control.Monad.IOSim.InternalTypes,
3233
Control.Monad.IOSimPOR.Internal,
3334
Control.Monad.IOSimPOR.QuickCheckUtils,
3435
Control.Monad.IOSimPOR.Timeout

io-sim/src/Control/Monad/IOSim/Internal.hs

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ import Control.Monad.Class.MonadTime
7878
import Control.Monad.Class.MonadTimer
7979

8080
import Control.Monad.IOSim.Types
81+
import Control.Monad.IOSim.InternalTypes
8182

8283
--
8384
-- Simulation interpreter
@@ -95,27 +96,6 @@ data Thread s a = Thread {
9596
threadNextTId :: !Int
9697
}
9798

98-
-- We hide the type @b@ here, so it's useful to bundle these two parts
99-
-- together, rather than having Thread have an extential type, which
100-
-- makes record updates awkward.
101-
data ThreadControl s a where
102-
ThreadControl :: SimA s b
103-
-> ControlStack s b a
104-
-> ThreadControl s a
105-
106-
data ControlStack s b a where
107-
MainFrame :: ControlStack s a a
108-
ForkFrame :: ControlStack s () a
109-
MaskFrame :: (b -> SimA s c) -- subsequent continuation
110-
-> MaskingState -- thread local state to restore
111-
-> ControlStack s c a
112-
-> ControlStack s b a
113-
CatchFrame :: Exception e
114-
=> (e -> SimA s b) -- exception continuation
115-
-> (b -> SimA s c) -- subsequent continuation
116-
-> ControlStack s c a
117-
-> ControlStack s b a
118-
11999
labelledTVarId :: TVar s a -> ST s (Labelled TVarId)
120100
labelledTVarId TVar { tvarId, tvarLabel } = (Labelled tvarId) <$> readSTRef tvarLabel
121101

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
{-# LANGUAGE GADTs #-}
2+
{-# LANGUAGE StandaloneDeriving #-}
3+
4+
-- | Internal types shared between `IOSim` and `IOSimPOR`.
5+
--
6+
module Control.Monad.IOSim.InternalTypes
7+
( ThreadControl (..)
8+
, ControlStack (..)
9+
) where
10+
11+
import Control.Exception (Exception)
12+
import Control.Monad.Class.MonadThrow (MaskingState (..))
13+
14+
import Control.Monad.IOSim.Types (SimA)
15+
16+
17+
-- We hide the type @b@ here, so it's useful to bundle these two parts
18+
-- together, rather than having Thread have an extential type, which
19+
-- makes record updates awkward.
20+
data ThreadControl s a where
21+
ThreadControl :: SimA s b
22+
-> ControlStack s b a
23+
-> ThreadControl s a
24+
25+
instance Show (ThreadControl s a) where
26+
show _ = "..."
27+
28+
data ControlStack s b a where
29+
MainFrame :: ControlStack s a a
30+
ForkFrame :: ControlStack s () a
31+
MaskFrame :: (b -> SimA s c) -- subsequent continuation
32+
-> MaskingState -- thread local state to restore
33+
-> ControlStack s c a
34+
-> ControlStack s b a
35+
CatchFrame :: Exception e
36+
=> (e -> SimA s b) -- exception continuation
37+
-> (b -> SimA s c) -- subsequent continuation
38+
-> ControlStack s c a
39+
-> ControlStack s b a
40+
41+
instance Show (ControlStack s b a) where
42+
show = show . dash
43+
where dash :: ControlStack s' b' a' -> ControlStackDash
44+
dash MainFrame = MainFrame'
45+
dash ForkFrame = ForkFrame'
46+
dash (MaskFrame _ m s) = MaskFrame' m (dash s)
47+
dash (CatchFrame _ _ s) = CatchFrame' (dash s)
48+
49+
data ControlStackDash =
50+
MainFrame'
51+
| ForkFrame'
52+
| MaskFrame' MaskingState ControlStackDash
53+
| CatchFrame' ControlStackDash
54+
deriving Show

io-sim/src/Control/Monad/IOSimPOR/Internal.hs

Lines changed: 1 addition & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ import Control.Monad.Class.MonadTime
8080
import Control.Monad.Class.MonadTimer
8181

8282
import Control.Monad.IOSim.Types
83+
import Control.Monad.IOSim.InternalTypes
8384
import Control.Monad.IOSimPOR.Timeout(unsafeTimeout)
8485

8586
--
@@ -104,45 +105,6 @@ data Thread s a = Thread {
104105
}
105106
deriving Show
106107

107-
-- We hide the type @b@ here, so it's useful to bundle these two parts
108-
-- together, rather than having Thread have an extential type, which
109-
-- makes record updates awkward.
110-
data ThreadControl s a where
111-
ThreadControl :: SimA s b
112-
-> ControlStack s b a
113-
-> ThreadControl s a
114-
115-
instance Show (ThreadControl s a) where
116-
show _ = "..."
117-
118-
data ControlStack s b a where
119-
MainFrame :: ControlStack s a a
120-
ForkFrame :: ControlStack s () a
121-
MaskFrame :: (b -> SimA s c) -- subsequent continuation
122-
-> MaskingState -- thread local state to restore
123-
-> ControlStack s c a
124-
-> ControlStack s b a
125-
CatchFrame :: Exception e
126-
=> (e -> SimA s b) -- exception continuation
127-
-> (b -> SimA s c) -- subsequent continuation
128-
-> ControlStack s c a
129-
-> ControlStack s b a
130-
131-
instance Show (ControlStack s b a) where
132-
show = show . dash
133-
where dash :: ControlStack s' b' a' -> ControlStackDash
134-
dash MainFrame = MainFrame'
135-
dash ForkFrame = ForkFrame'
136-
dash (MaskFrame _ m s) = MaskFrame' m (dash s)
137-
dash (CatchFrame _ _ s) = CatchFrame' (dash s)
138-
139-
data ControlStackDash =
140-
MainFrame'
141-
| ForkFrame'
142-
| MaskFrame' MaskingState ControlStackDash
143-
| CatchFrame' ControlStackDash
144-
deriving Show
145-
146108
isTestThreadId :: ThreadId -> Bool
147109
isTestThreadId (TestThreadId _) = True
148110
isTestThreadId _ = False

0 commit comments

Comments
 (0)