File size: 7,959 Bytes
72cfe15
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
'''
RASP OPERATORS THAT ARE SUPPORTED BY TRACR'S PYTHON EMBEDDING
This file contains Python classes that define the rasp operators supported by TRACR's python embedding of the langauge.
This is subset of everything that TRACR supports in python, due to project time constraints.
'''
import random
from typing import (Any, Callable, Dict, Generic, List, Mapping, Optional,
                    Sequence, TypeVar, Union)
from tracr.rasp import rasp
import subprocess
import time

'''
CLASS DEFINITIONS
'''
class Tokens:
    '''
    Tokens constant.
    '''
    def __init__(self):
        self.n_args = 0
        self.arg_types = []
        self.return_type = rasp.SOp
        self.weight = 1
    
    def to_python(self):
        # return an object that can be compiled into a TRACR transformer
        # arguments should be python objects
        return rasp.tokens
    
    def str(self):
        # represent rasp operator in string form
        # expects arguments to be strings
        return "tokens"
    
class Indices:
    def __init__(self):
        self.n_args = 0
        self.arg_types = []
        self.return_type = rasp.SOp
        self.weight = 1
    
    def to_python(self):
        # return an object that can be compiled into a TRACR transformer
        # arguments should be python objects
        return rasp.indices
    
    def str(self):
        # represent rasp operator in string form
        # expects arguments to be strings
        return "indices"

class Zero:
    def __init__(self):
        self.n_args = 0
        self.arg_types = []
        self.return_type = int
        self.weight = 1
    
    def to_python(self):
        # return an object that can be compiled into a TRACR transformer
        # arguments should be python objects
        return 0
    
    def str(self):
        # represent rasp operator in string form
        # expects arguments to be strings
        return "0"

class One:
    def __init__(self):
        self.n_args = 0
        self.arg_types = []
        self.return_type = int
        self.weight = 1
    
    def to_python(self):
        # return an object that can be compiled into a TRACR transformer
        # arguments should be python objects
        return 1
    
    def str(self):
        # represent rasp operator in string form
        # expects arguments to be strings
        return "1"

class Equal:
    '''
    Comparison Equal constant.
    '''
    def __init__(self):
        self.n_args = 0
        self.arg_types = []
        self.return_type = rasp.Predicate
        self.weight = 1
    
    def to_python(self):
        # return an object that can be compiled into a TRACR transformer
        # arguments should be python objects
        return rasp.Comparison.EQ
    
    def str(self):
        # represent rasp operator in string form
        # expects arguments to be strings
        return "=="

class GT:
    '''
    Greater Than comparison operator.
    '''
    pass

class LT:
    '''
    Less Than comparison operator
    '''
    pass

class LEQ:
    pass

class GEQ:
    pass

class TRUE:
    '''
    Comparison True constant.
    '''
    def __init__(self):
        self.n_args = 0
        self.arg_types = []
        self.return_type = rasp.Predicate
        self.weight = 1
    
    def to_python(self):
        # return an object that can be compiled into a TRACR transformer
        # arguments should be python objects
        return rasp.Comparison.TRUE
    
    def str(self):
        # represent rasp operator in string form
        # expects arguments to be strings
        return "true"

class FALSE:
    pass

class Add:
    '''
    Element-wise.
    Input can be either int, float or s-op.
    '''
    pass

class Subtract:
    '''
    Element-wise.
    Input can be either int, float or s-op.
    '''
    def __init__(self):
        self.n_args = 2
        self.arg_types = [Union[rasp.SOp, float, int], Union[rasp.SOp, float, int]]
        self.return_type = Union[rasp.SOp, int, float]
        self.weight = 1
    
    def to_python(self, x, y):
        # return an object that can be compiled into a TRACR transformer
        # arguments should be python objects
        if type(x) == type(rasp.tokens):
            return None
        if type(y) == type(rasp.tokens):
            return None
        return x - y
    
    def str(self, x, y):
        # represent rasp operator in string form
        # expects arguments to be strings
        return f"{x} - {y}"

class Mult:
    '''
    Element-wise.
    Input can be either int, float or s-op.
    '''
    pass

class Divide:
    '''
    Element-wise.
    Input can be either int, float or s-op.
    '''
    pass

class Fill:
    '''
    Given fill value and length, returns Sop of that length with that fill value.
    Fill value can be int, float, or char.
    Length must be a positive integer.
    '''
    pass

class SelectorAnd:
    '''
    Input can be bool or s-op.
    '''
    pass

class SelectorOr:
    '''
    Input can be bool or s-op.
    '''
    pass

class SelectorNot:
    '''
    Input is an s-op of bools. (Or bool-convertible values.)
    '''
    pass

class Select:
    '''
    Select operator.
    '''
    def __init__(self):
        self.n_args = 3
        self.arg_types = [rasp.SOp, rasp.SOp, rasp.Predicate]
        self.return_type = rasp.Selector 
        self.weight = 1
    
    def to_python(self, sop1, sop2, comp):
        # return an object that can be compiled into a TRACR transformer
        # arguments should be python objects
        return rasp.Select(sop1, sop2, comp)
    
    def str(self, sop1, sop2, comp):
        # represent rasp operator in string form
        # expects arguments to be strings
        return f"select({sop1}, {sop2}, {comp})"

class Aggregate:
    '''
    The Aggregate operator.
    '''
    def __init__(self):
        self.n_args = 2
        self.arg_types = [rasp.Selector, rasp.SOp]
        self.return_type = rasp.SOp
        self.weight = 1
    
    def to_python(self, sel, sop):
        # return an object that can be compiled into a TRACR transformer
        # arguments should be python objects
        return rasp.Aggregate(sel, sop)
    
    def str(self, sel, sop):
        # represent rasp operator in string form
        # expects arguments to be strings
        return f"aggregate({sel}, {sop})"

class SelectorWidth:
    '''
    The selector_width operator.
    '''
    def __init__(self):
        self.n_args = 1
        self.arg_types = [rasp.Selector]
        self.return_type = rasp.SOp 
        self.weight = 1
    
    def to_python(self, sel):
        # return an object that can be compiled into a TRACR transformer
        # arguments should be python objects
        return rasp.SelectorWidth(sel)
    
    def str(self, sel):
        # represent rasp operator in string form
        # expects arguments to be strings
        return f"select_width({sel})"

'''
GLOBAL CONSTANTS
'''

# define operators
rasp_operators = [Select(), SelectorWidth(), Aggregate(), Subtract()]
rasp_consts = [Tokens(), Tokens(), Equal(), TRUE(), Indices(), Indices(), Zero(), One()]
'''
TESTING
'''
if __name__ == "__main__":
    test_select = Select()
    
    test_select_python = test_select.to_python(Tokens().to_python(), Tokens().to_python(), Equal().to_python())
    actual_ts_python = rasp.Select(rasp.tokens, rasp.tokens, rasp.Comparison.EQ)
    assert type(Tokens().to_python()) == type(rasp.tokens)
    assert type(Equal().to_python() == type(rasp.Comparison.EQ))
    assert type(test_select_python) == type(actual_ts_python)
    
    test_select_string = test_select.str(Tokens().str(), Tokens().str(), Equal().str())
    actual_ts_string = "select(tokens, tokens, ==)"
    assert(test_select_string == actual_ts_string)
    
    
    test_aggregate = Aggregate()
    print(rasp.Aggregate(rasp.Select(rasp.tokens, rasp.tokens, rasp.Comparison.EQ), rasp.tokens)("hi"))
    
    print("all tests passed hooray!")