-
Notifications
You must be signed in to change notification settings - Fork 1
/
Node.hs
95 lines (73 loc) · 2.44 KB
/
Node.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
{-# LANGUAGE GADTs, StandaloneDeriving #-}
module Node where
import Distribution
import Control.Monad.IO.Class
-- | "flag" passed from user
data Logged = Logged { logName :: String }
| Unlogged
deriving (Show)
class (Show n, NodeClass n) => SrcNode n where
addTrans :: Transition -> n -> n
class (Show n, NodeClass n) => DstNode n where
type NodeId = Integer
type Transition = (DestNode, Distr)
data NodeInfo = NodeInfo { nodeId :: NodeId
, nodeLogged :: Logged }
deriving (Show)
data Source = Source NodeInfo [Transition]
deriving (Show)
instance SrcNode Source where
addTrans t (Source inf ts) = Source inf (t : ts)
data Sink = Sink NodeInfo Distr
deriving (Show)
instance DstNode Sink where
data Inter = Inter NodeInfo [Transition]
deriving (Show)
instance SrcNode Inter where
addTrans t (Inter inf ts) = Inter inf (t : ts)
instance DstNode Inter where
data AnyNode = AnyInter Inter
| AnySource Source
| AnySink Sink
deriving (Show)
foldAny :: (Inter -> a) -> (Source -> a) -> (Sink -> a) -> AnyNode -> a
foldAny a b c d = case d of
AnyInter x -> a x
AnySource x -> b x
AnySink x -> c x
data SourceNode where
MkSourceNode :: (NodeClass n, SrcNode n) => n -> SourceNode
deriving instance Show SourceNode
instance SrcNode SourceNode where
addTrans t (MkSourceNode n) = MkSourceNode (addTrans t n)
data DestNode where
MkDestNode :: (NodeClass n, DstNode n) => n -> DestNode
deriving instance Show DestNode
class (Show n) => NodeClass n where
nodeInfo :: n -> NodeInfo
transitions :: n -> [Transition]
toAny :: n -> AnyNode
instance NodeClass Sink where
nodeInfo (Sink n _) = n
transitions _ = []
toAny = AnySink
instance NodeClass Source where
nodeInfo (Source n _) = n
transitions (Source _ ts) = ts
toAny = AnySource
instance NodeClass Inter where
nodeInfo (Inter n _) = n
transitions (Inter _ ts) = ts
toAny = AnyInter
instance NodeClass AnyNode where
nodeInfo a = foldAny nodeInfo nodeInfo nodeInfo a
transitions a = foldAny transitions transitions transitions a
toAny = id
instance NodeClass DestNode where
nodeInfo (MkDestNode n) = nodeInfo n
transitions (MkDestNode n) = transitions n
toAny (MkDestNode n) = toAny n
instance NodeClass SourceNode where
nodeInfo (MkSourceNode n) = nodeInfo n
transitions (MkSourceNode n) = transitions n
toAny (MkSourceNode n) = toAny n