Skip to content

statics-names.stx

pdmosses/metaborg-poosl/org.metaborg.lang.poosl/trans/statics-names.stx

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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
module 

imports
    signatures/Poosl-sig
    signatures/Common-sig

signature

    name-resolution
        labels  // Parent
        labels  // Inheritance
        // $ = local scope

    sorts  constructors
         : scope -> TypeProcessOrClusterClass
        TypeProcessClassRoot : TypeProcessOrClusterClass
         : scope * scope -> TypeProcessOrClusterClass

    sorts  constructors
         : TypeDataClass
         : scope * TypeDataClass -> TypeDataClass

    sorts  constructors
         : list(TypeDataClass) * list(TypeDataClass) -> TypeProcessMethod

    sorts  constructors
         : TypeDataClass * list(TypeDataClass) * TypeDataClass -> TypeDataMethod

    sorts  constructors
         : list(TypeDataClass) -> TypeMessage

    relations
         : string -> TypeProcessOrClusterClass
         : string -> TypeDataClass
         : (string * int * int) -> TypeProcessMethod
         : (string * string * int) -> TypeDataMethod
        : (string * UnaryOperator * int) -> TypeDataMethod
         : (string * OperatorBinary * int) -> TypeDataMethod
         : (string * string * int) -> TypeMessage
         : (string * string * int) -> TypeMessage
    relations
         : string -> TypeProcessOrClusterClass
         : string
         : string -> TypeDataClass

rules // --- ProcessOrClusterClass -------

     : scope * string -> list((path * (string * TypeProcessOrClusterClass)))
    resolveProcessOrClusterClass(s, x) = ps :-
        query varProcessOrClusterClass
            filter P* and { x' :- x' == x }
            min /* Label order: */ and /* Shadow: */ true
            in /* Scope: */ s |-> /* Result: */ ps.

     : scope * string * TypeProcessOrClusterClass
    declareProcessOrClusterClass(, , ) :-
        !varProcessOrClusterClass[x, T] in s,
        resolveProcessOrClusterClass(s, x) == [(_, (_, _))] | error $[Duplicate definition of process or cluster class [x]],
        @x.type := T, @x.decl := x.

     : scope * string -> TypeProcessOrClusterClass
    typeOfProcessOrClusterClass(s, ) = T :- {} @x.ref := x',
        resolveProcessOrClusterClass(s, x) == [(_,(x', T))|_] | error $[Process or cluster class [x] not defined].

rules // --- DataClass -------

     : scope * string -> list((path * (string * TypeDataClass)))
    resolveDataClass(s, x) = ps :-
        query varDataClass
            filter P* and { x' :- x' == x }
            min /* Label order: */ and /* Shadow: */ true
            in /* Scope: */ s |-> /* Result: */ ps.

     : scope * string * TypeDataClass
    declareDataClass(, , ) :-
        !varDataClass[x, T] in s,
        resolveDataClass(s, x) == [(_, (_, _))] | error $[Duplicate definition of data class [x]],
        @x.type := T, @x.decl := x.

    typeOfDataClasses maps typeOfDataClass(*, list(*)) = list(*)
     : scope * string -> TypeDataClass
    typeOfDataClass(s, ) = T :- {} @x.ref := x',
        resolveDataClass(s, x) == [(_,(x', T))|_] | error $[Data class [x] not defined].

     : scope * string -> TypeDataClass
    typeOfDataClassNoRef(s, ) = T :- {x'}
        resolveDataClass(s, x) == [(_,(x', T))|_] | error $[Data class [x] not defined].

rules // --- ProcessMethod -------

     : scope * (string * int * int) -> list((path * ((string * int * int) * TypeProcessMethod)))
    resolveProcessMethod(s, x) = ps :-
        query varProcessMethod
            filter P* I* and { x' :- x' == x }
            min /* Label order: */ $ < P, $ < I, P < I and /* Shadow: */ true // prefer P over I
            in /* Scope: */ s |-> /* Result: */ ps.

     : scope * string * TypeProcessMethod
    declareProcessMethod(, , ) :-
        { } T == TypeProcessMethod(inArgs, outArgs),
        { } i == lengthDataClasses(inArgs), j == lengthDataClasses(outArgs),
        !varProcessMethod[(x, i, j), T] in s,
        resolveProcessMethod(s, (x, i, j)) == [(_, (_, _))] | error $[Duplicate definition of process method [x] with [i] input argument(s) and [j] input argument(s)],
        @x.type := T, @x.decl := x.

     : scope * string * int * int -> TypeProcessMethod
    typeOfProcessMethod(s, , , ) = T :- { i' j'} @x.ref := x',
        resolveProcessMethod(s, (x, i, j)) == [(_,((x', i', j'), T))|_] | error $[Process method [x] with [i] input argument(s) and [j] input argument(s) not defined].

rules // --- NamedDataMethod -------

     : scope * (string * string * int) -> list((path * ((string * string * int) * TypeDataMethod)))
    resolveNamedDataMethod(s, x) = ps :-
        query varDataMethodNamed
            filter P* and { x' :- x' == x }
            min /* Label order: */ and /* Shadow: */ true
            in /* Scope: */ s |-> /* Result: */ ps.

     : scope * string * int -> list((path * ((string * string * int) * TypeDataMethod)))
    resolveNamedDataMethodPartial(s, x, i) = ps :-
        query varDataMethodNamed
            filter P* and { (c', x', i') :- (x', i') == (x, i) }
            min /* Label order: */ and /* Shadow: */ true
            in /* Scope: */ s |-> /* Result: */ ps.

     : scope * string * string * TypeDataMethod
    declareNamedDataMethod(, , , ) :-
        {receiver  retType} T == TypeDataMethod(receiver, args, retType),
        {} i == lengthDataClasses(args),
        !varDataMethodNamed[(c, x, i), T] in s,
        resolveNamedDataMethod(s, (c, x, i)) == [(_, (_, _))] | error $[Duplicate definition of named data method [x] with [i] argument(s)],
        @x.type := T, @x.decl := x.

     : scope * string * int -> list(TypeDataMethod)
    typeOfNamedDataMethods(s, , ) = getTypeNamedDataMethods([T|Ts]) :-
        resolveNamedDataMethodPartial(s, x, i) == [T|Ts] | error $[Named data method [x] with [i] argument(s) not defined].

    getTypeNamedDataMethods maps getTypeNamedDataMethod(list(*)) = list(*)
     : (path * ((string * string * int) * TypeDataMethod)) -> TypeDataMethod
    getTypeNamedDataMethod((p, (_, t))) = t.

rules // --- UnaryDataMethod -------

     : scope * (string * UnaryOperator * int) -> list((path * ((string * UnaryOperator * int) * TypeDataMethod)))
    resolveUnaryDataMethod(s, x) = ps :-
        query varDataMethodUnary
            filter P* and { x' :- x' == x }
            min /* Label order: */ and /* Shadow: */ true
            in /* Scope: */ s |-> /* Result: */ ps.

     : scope * UnaryOperator * int -> list((path * ((string * UnaryOperator * int) * TypeDataMethod)))
    resolveUnaryDataMethodPartial(s, x, i) = ps :-
        query varDataMethodUnary
            filter P* and { (c', x', i') :- (x', i') == (x, i) }
            min /* Label order: */ and /* Shadow: */ true
            in /* Scope: */ s |-> /* Result: */ ps.

     : scope * string * UnaryOperator * TypeDataMethod
    declareUnaryDataMethod(, , , ) :-
        {receiver  retType} T == TypeDataMethod(receiver, args, retType),
        {} i == lengthDataClasses(args),
        !varDataMethodUnary[(c, x, i), T] in s,
        resolveUnaryDataMethod(s, (c, x, i)) == [(_, (_, _))] | error $[Duplicate definition of unary data method [x]],
        @x.type := T, @x.decl := x.

     : scope * UnaryOperator -> list(TypeDataMethod)
    typeOfUnaryDataMethods(s, ) = getTypeUnaryDataMethods([T|Ts]) :-
        resolveUnaryDataMethodPartial(s, x, 0) == [T|Ts] | error $[Unary data method [x] not defined].

    getTypeUnaryDataMethods maps getTypeUnaryDataMethod(list(*)) = list(*)
     : (path * ((string * UnaryOperator * int) * TypeDataMethod)) -> TypeDataMethod
    getTypeUnaryDataMethod((p, (_, t))) = t.

rules // --- BinaryDataMethod -------

     : scope * (string * OperatorBinary * int) -> list((path * ((string * OperatorBinary * int) * TypeDataMethod)))
    resolveBinaryDataMethod(s, x) = ps :-
        query varDataMethodBinary
            filter P* and { x' :- x' == x }
            min /* Label order: */ and /* Shadow: */ true
            in /* Scope: */ s |-> /* Result: */ ps.

     : scope * OperatorBinary * int -> list((path * ((string * OperatorBinary * int) * TypeDataMethod)))
    resolveBinaryDataMethodPartial(s, x, i) = ps :-
        query varDataMethodBinary
            filter P* and { (c', x', i') :- (x', i') == (x, i) }
            min /* Label order: */ and /* Shadow: */ true
            in /* Scope: */ s |-> /* Result: */ ps.

     : scope * string * OperatorBinary * TypeDataMethod
    declareBinaryDataMethod(, , , ) :-
        {receiver  retType} T == TypeDataMethod(receiver, args, retType),
        {} i == lengthDataClasses(args),
        !varDataMethodBinary[(c, x, i), T] in s,
        resolveBinaryDataMethod(s, (c, x, i)) == [(_, (_, _))] | error $[Duplicate definition of binary data method [x]],
        @x.type := T, @x.decl := x.

     : scope * OperatorBinary -> list(TypeDataMethod)
    typeOfBinaryDataMethods(s, ) = getTypeBinaryDataMethods([T|Ts]) :-
        resolveBinaryDataMethodPartial(s, x, 1) == [T|Ts] | error $[Binary data method [x] not defined].

    getTypeBinaryDataMethods maps getTypeBinaryDataMethod(list(*)) = list(*)
     : (path * ((string * OperatorBinary * int) * TypeDataMethod)) -> TypeDataMethod
    getTypeBinaryDataMethod((p, (_, t))) = t.


rules // --- ReceiveMessage -------

     : scope * (string * string * int) -> list((path * ((string * string * int) * TypeMessage)))
    resolveReceiveMessage(s, x) = ps :-
        query varReceiveMessage
            filter P* I* and { x' :- x' == x }
            min /* Label order: */ and /* Shadow: */ true
            in /* Scope: */ s |-> /* Result: */ ps.

     : scope * string * string * TypeMessage
    declareReceiveMessage(, , , ) :-
        {} T == TypeMessage(args),
        {} i == lengthDataClasses(args),
        !varReceiveMessage[(p, m, i), T] in s,
        resolveReceiveMessage(s, (p, m, i)) == [(_, (_, _))] | error $[Duplicate definition of receive message [p].[m] with [i] argument(s)],
        @m.type := T, @m.decl := m.

     : scope * string * string * int -> TypeMessage
    typeOfReceiveMessage(s, , , ) = T :- {p'  i'} @m.ref := m',
        resolveReceiveMessage(s, (p, m, i)) == [(_,((p', m', i'), T))|_] | error $[Receive message [p].[m] with [i] argument(s) not defined].

rules // --- SendMessage -------

     : scope * (string * string * int) -> list((path * ((string * string * int) * TypeMessage)))
    resolveSendMessage(s, x) = ps :-
        query varSendMessage
            filter P* I* and { x' :- x' == x }
            min /* Label order: */ and /* Shadow: */ true
            in /* Scope: */ s |-> /* Result: */ ps.

     : scope * string * string * TypeMessage
    declareSendMessage(, , , ) :-
        {} T == TypeMessage(args),
        {} i == lengthDataClasses(args),
        !varSendMessage[(p, m, i), T] in s,
        resolveSendMessage(s, (p, m, i)) == [(_, (_, _))] | error $[Duplicate definition of send message [p].[m] with [i] argument(s)],
        @m.type := T, @m.decl := m.

     : scope * string * string * int -> TypeMessage
    typeOfSendMessage(s, , , ) = T :- {p'  i'} @m.ref := m',
        resolveSendMessage(s, (p, m, i)) == [(_,((p', m', i'), T))|_] | error $[Send message [p].[m] with [i] argument(s) not defined].

rules // --- Instance -------

     : scope * string -> list((path * (string * TypeProcessOrClusterClass)))
    resolveInstance(s, x) = ps :-
        query varInstance
            filter P* and { x' :- x' == x }
            min /* Label order: */ and /* Shadow: */ true
            in /* Scope: */ s |-> /* Result: */ ps.

     : scope * string * TypeProcessOrClusterClass
    declareInstance(, , ) :-
        !varInstance[x, T] in s,
        resolveInstance(s, x) == [(_, (_, _))] | error $[Duplicate definition of instance [x]],
        @x.type := T, @x.decl := x.

     : scope * string -> TypeProcessOrClusterClass
    typeOfInstance(s, ) = T :- {} @x.ref := x',
        resolveInstance(s, x) == [(_,(x', T))|_] | error $[Instance [x] not defined].

rules // --- Port -------

     : scope * string -> list((path * string))
    resolvePort(s, x) = ps :-
        query varPort
            filter P* I* and { x' :- x' == x }
            min /* Label order: */ and /* Shadow: */ true
            in /* Scope: */ s |-> /* Result: */ ps.

     : scope * string
    declarePort(, ) :-
        !varPort[x] in s,
        resolvePort(s, x) == [(_, _)] | error $[Duplicate definition of port [x]],
        @x.decl := x.

     : scope * string
    typeOfPort(s, ) :- {} @x.ref := x',
        resolvePort(s, x) == [(_,x')|_] | error $[Port [x] not defined].

rules // --- Variable -------

     : scope * string -> list((path * (string * TypeDataClass)))
    resolveVariable(s, x) = ps :-
        query varVariable
            filter P* I* and { x' :- x' == x }
            min /* Label order: */ and /* Shadow: */ true
            in /* Scope: */ s |-> /* Result: */ ps.

     maps declareVariable(*, list(*), *)
     : scope * string * TypeDataClass
    declareVariable(, , ) :-
        !varVariable[x, T] in s,
        resolveVariable(s, x) == [(_, (_, _))] | error $[Duplicate definition of variable [x]],
        @x.type := T, @x.decl := x.

     maps typeOfVariable(*, list(*)) = list(*)
     : scope * string -> TypeDataClass
    typeOfVariable(s, ) = T :- {} @x.ref := x',
        resolveVariable(s, x) == [(_,(x', T))|_] | error $[Variable [x] not defined].

rules   // === List operations on TypeDataClass =======

     : list(TypeDataClass) * list(TypeDataClass) -> list(TypeDataClass)
    concatDataClasses([], ds2) = ds2.
    concatDataClasses([d1|ds1], ds2) = [d1|concatDataClasses(ds1, ds2)]. 

     : list(list(TypeDataClass)) -> list(TypeDataClass)
    flattenDataClasses([]) = [].
    flattenDataClasses([d|ds]) = concatDataClasses(d, flattenDataClasses(ds)).

    : list(TypeDataClass) -> int
    lengthDataClasses([]) = 0.
    lengthDataClasses([_|t]) = len :- {t_len}
      t_len == lengthDataClasses(t),
      len #= t_len + 1.