@@ -7,35 +7,66 @@ module Syntax.Concrete where
7
7
8
8
import qualified Syntax.Abstract as A
9
9
import qualified Type as A
10
+ import Type (HasDual (.. ))
10
11
import Data.Text (Text )
11
- import Data.Loc (Loc , Located (.. ))
12
+ import Data.Loc (Loc ( .. ) , Located (.. ))
12
13
import Prelude hiding (LT , EQ , GT )
14
+ import Data.Function (on )
13
15
14
16
--------------------------------------------------------------------------------
15
17
-- | Concrete Syntax Tree
16
18
17
19
data Label = Label Text Loc
18
20
deriving (Show )
21
+
22
+ instance Eq Label where
23
+ (==) = (==) `on` toAbstract
24
+ instance Ord Label where
25
+ compare = compare `on` toAbstract
26
+
19
27
data Chan = Pos Text Loc
20
28
| Neg Text Loc
21
29
| Res Text Loc
22
30
deriving (Show )
23
31
32
+ instance Eq Chan where
33
+ (==) = (==) `on` toAbstract
34
+ instance Ord Chan where
35
+ compare = compare `on` toAbstract
36
+ instance HasDual Chan where
37
+ dual (Pos x l) = Neg x l
38
+ dual (Neg x l) = Pos x l
39
+ dual (Res " stdout" l) = Res " stdin" l
40
+ dual (Res " stdin" l) = Res " stdout" l
41
+ dual (Res x l) = Res x l
42
+
24
43
data Program = Program [Definition ] Loc
25
44
deriving (Show )
26
45
data ProcName = ProcName Text Loc
27
46
deriving (Show )
47
+
48
+ instance Eq ProcName where
49
+ (==) = (==) `on` toAbstract
50
+ instance Ord ProcName where
51
+ compare = compare `on` toAbstract
52
+
28
53
data TypeName = TypeName Text Loc
29
54
deriving (Show )
30
55
56
+ instance Eq TypeName where
57
+ (==) = (==) `on` toAbstract
58
+ instance Ord TypeName where
59
+ compare = compare `on` toAbstract
60
+
61
+
31
62
data Definition = ProcDefn ProcName Proc Loc
32
63
| ChanType Chan Type Loc
33
64
| TypeDefn TypeName Type Loc
34
65
deriving (Show )
35
66
36
67
data Proc = Send Chan Expr Proc Loc
37
68
| Recv Chan [Clause ] Loc
38
- | Nu ProcName (Maybe Type ) Proc Loc
69
+ | Nu Chan (Maybe Type ) Proc Loc
39
70
| Par Proc Proc Loc
40
71
| Call ProcName Loc
41
72
| Repl Proc Loc
@@ -75,7 +106,10 @@ data Expr = ExprTuple [Expr] Loc
75
106
76
107
data TypeVar = TypeVarIndex Int Loc
77
108
| TypeVarText TypeName Loc
78
- deriving (Show )
109
+ deriving (Show )
110
+
111
+ instance Eq TypeVar where
112
+ (==) = (==) `on` toAbstract
79
113
80
114
data BaseType = BaseInt Loc
81
115
| BaseBool Loc
@@ -91,9 +125,72 @@ data Type = TypeEnd Loc
91
125
| TypeVar TypeVar Loc
92
126
| TypeMu Type Loc
93
127
deriving (Show )
94
- data TypeOfLabel = TypeOfLabel Label Type Loc
128
+
129
+ -- type substitution. it is assumed that s contains
130
+ -- no bound variables.
131
+
132
+ substTypeOfLabel :: Int -> Type -> TypeOfLabel -> TypeOfLabel
133
+ substTypeOfLabel i s (TypeOfLabel label t l) = TypeOfLabel label (substType i s t) l
134
+
135
+ substType :: Int -> Type -> Type -> Type
136
+ substType _ _ (TypeEnd l) = TypeEnd l
137
+ substType _ _ (TypeBase t l) = TypeBase t l
138
+ substType i s (TypeTuple ts l) = TypeTuple (map (substType i s) ts) l
139
+ substType i s (TypeSend t u l) = TypeSend (substType i s t) (substType i s u) l
140
+ substType i s (TypeRecv t u l) = TypeRecv (substType i s t) (substType i s u) l
141
+ substType i s (TypeChoi ts l) = TypeChoi (map (substTypeOfLabel i s) ts) l
142
+ substType i s (TypeSele ts l) = TypeSele (map (substTypeOfLabel i s) ts) l
143
+ substType i s (TypeUn t l) = TypeUn (substType i s t) l
144
+ substType i s (TypeVar j l) | TypeVarIndex i l == j = s
145
+ | otherwise = TypeVar j l
146
+ substType i s (TypeMu t l) = TypeMu (substType (1 + i) s t) l
147
+
148
+ unfoldType :: Type -> Type
149
+ unfoldType (TypeMu t l) = substType 0 (TypeMu t l) t
150
+ unfoldType t = t
151
+
152
+ stripUnrestricted :: Type -> (Type , Bool )
153
+ stripUnrestricted (TypeEnd l) = (TypeEnd l, True )
154
+ stripUnrestricted (TypeBase t l) = (TypeBase t l, True )
155
+ stripUnrestricted (TypeUn t l) = (t, True ) -- shouldn't be nested
156
+ stripUnrestricted (TypeTuple ts l) = (TypeTuple (map fst tts) l, and (map snd tts))
157
+ where tts = map stripUnrestricted ts
158
+ stripUnrestricted (TypeMu t l) = let (t', p) = stripUnrestricted t in (TypeMu t' l, p)
159
+ stripUnrestricted t = (t, False )
160
+
161
+ tInt :: Type
162
+ tInt = TypeBase (BaseInt NoLoc ) NoLoc
163
+
164
+ tBool :: Type
165
+ tBool = TypeBase (BaseBool NoLoc ) NoLoc
166
+
167
+ instance Eq Type where
168
+ (==) = (==) `on` toAbstract
169
+
170
+ instance Ord Type where
171
+ compare = compare `on` toAbstract
172
+
173
+ instance HasDual Type where
174
+ dual (TypeEnd l) = TypeEnd l
175
+ dual (TypeBase t l) = TypeBase t l
176
+ dual (TypeTuple ts l) = TypeTuple (map dual ts) l
177
+ dual (TypeSend c t l) = TypeRecv c (dual t) l
178
+ dual (TypeRecv c t l) = TypeSend c (dual t) l
179
+ dual (TypeSele ts l) = TypeChoi (map dual ts) l
180
+ dual (TypeChoi ts l) = TypeSele (map dual ts) l
181
+ dual (TypeUn t l) = TypeUn (dual t) l
182
+ dual (TypeVar i l) = TypeVar i l
183
+ dual (TypeMu t l) = TypeMu (dual t) l
184
+
185
+ data TypeOfLabel = TypeOfLabel Label Type Loc
95
186
deriving (Show )
96
187
188
+ instance HasDual TypeOfLabel where
189
+ dual (TypeOfLabel label t l) = TypeOfLabel label (dual t) l
190
+
191
+ typedLabelToPair :: TypeOfLabel -> (Label , Type )
192
+ typedLabelToPair (TypeOfLabel l t _) = (l, t)
193
+
97
194
--------------------------------------------------------------------------------
98
195
-- | Instance of Located
99
196
0 commit comments