-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuiltin.js
570 lines (516 loc) · 17.5 KB
/
builtin.js
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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
/**
* Type information on Builtin Functions
*/
import * as ST from './types.js';
import {Error} from './syntax.js';
import * as ER from './general.js';
/**
* Each BI class includes the arity of the builtin function.
* The types of arguments it would accept.
* It's function type.
*/
/**
* Polymorphic Builtins
*/
export class BI_eq {
constructor() {
this.arity = 2;
this.types = [new ST.String(), new ST.BNum()].concat(ST.allInts).concat(ST.allUints).concat(ST.allBystr).concat(ST.allAddr);
this.funTyp =
new ST.PolyFun("'A",
new ST.FunType(new ST.TypeVar("'A"),
new ST.FunType(new ST.TypeVar("'A"), new ST.ADT("Bool", []))));
}
}
export class BI_concat {
constructor() {
this.arity = 2;
this.types = [new ST.String()].concat(ST.allBystr).concat(ST.allAddr);
this.funTyp = new ST.PolyFun("'A",
new ST.FunType(new ST.TypeVar("'A"),
new ST.FunType(new ST.TypeVar("'A"), new ST.TypeVar("'A"))));
}
}
export class BI_substr {
constructor() {
this.arity = 3;
this.types = [new ST.String()].concat(ST.allBystr).concat(ST.allAddr);
this.funTyp =
new ST.PolyFun("'A",
new ST.FunType(new ST.TypeVar("'A"),
new ST.FunType(new ST.Uint32(),
new ST.FunType(new ST.Uint32(), new ST.TypeVar("'A")))));
}
}
export class BI_strlen{
constructor() {
this.arity = 1;
this.types = [new ST.String()].concat(ST.allBystr).concat(ST.allAddr);
this.funTyp = new ST.PolyFun("'A", new ST.FunType(new ST.TypeVar("'A"), new ST.Uint32()));
}
}
export class BI_to_string {
constructor() {
this.arity = 1;
this.types = [new ST.String()].concat(ST.allBystr).concat(ST.allAddr).concat(ST.allInts).concat(ST.allUints);
this.funTyp = new ST.PolyFun("'A", new ST.FunType(new ST.TypeVar("'A"), new ST.String));
}
}
export class BI_to_ascii{
constructor() {
this.arity = 1;
this.types = [new ST.String()].concat(ST.allBystr).concat(ST.allAddr);
this.funTyp = new ST.PolyFun("'A", new ST.FunType(new ST.TypeVar("'A"), new ST.String));
}
}
export class BI_strrev {
constructor() {
this.arity = 1;
this.types = [new ST.String()].concat(ST.allBystr).concat(ST.allAddr);
this.funTyp = new ST.PolyFun("'A", new ST.FunType(new ST.TypeVar("'A"), new ST.TypeVar("'A")));
}
}
export class BI_to_bystrx {
constructor(i) {
this.arity = 1;
this.types = ST.allBystr.concat(ST.allUints);
this.funTyp = new ST.PolyFun("'A", new ST.FunType(new ST.TypeVar("'A"), new ST.ADT("Option", new ST.ByStrXTyp(i))));
}
}
export class BI_to_bystr {
constructor() {
this.arity = 1;
this.types = [new ST.ByStrXTyp(20)];
this.funTyp = new ST.FunType(new ST.ByStrXTyp(20), new ST.ByStrTyp());
}
}
/**
* Block Numbers (skip most)
*/
export class BI_blt {
constructor() {
this.arity = 2;
this.types = [ST.BNum];
this.funTyp = new ST.FunType(new ST.BNum, new ST.FunType(new ST.BNum, new ST.ADT("Bool", [])));
}
}
/**
* CryptoBuiltins (skip)
*/
/**
* MapBuiltins
*/
export class BI_contains {
constructor() {
this.arity = 2;
this.types = [new ST.MapType()];
this.funTyp =
new ST.PolyFun("'K",
new ST.PolyFun("'V",
new ST.FunType(new ST.MapType(new ST.TypeVar("'K"), new ST.TypeVar("'V")),
new ST.FunType(new ST.TypeVar("'K"), new ST.ADT("Bool", [])))));
}
}
export class BI_put {
constructor() {
this.arity = 3;
this.types = [new ST.MapType()];
this.funTyp =
new ST.PolyFun("'K", new ST.PolyFun("'V",
new ST.FunType(new ST.MapType(new ST.TypeVar("'K"), new ST.TypeVar("'V")),
new ST.FunType(new ST.TypeVar("'K"),
new ST.FunType(new ST.TypeVar("'V"), new ST.MapType(new ST.TypeVar("'K"), new ST.TypeVar("'V")))))));
}
}
export class BI_get {
constructor() {
this.arity = 2;
this.types = [new ST.MapType()];
this.funTyp =
new ST.PolyFun("'K",
new ST.PolyFun("'V",
new ST.FunType(new ST.MapType(new ST.TypeVar("'K"), new ST.TypeVar("'V")),
new ST.FunType(new ST.TypeVar("'K"), new ST.ADT("Option", [new ST.TypeVar("'V")])))));
}
}
export class BI_remove {
constructor() {
this.arity = 2;
this.types = [new ST.MapType()];
this.funTyp =
new ST.PolyFun("'K",
new ST.PolyFun("'V",
new ST.FunType(new ST.MapType(new ST.TypeVar("'K"), new ST.TypeVar("'V")),
new ST.FunType(new ST.TypeVar("'K"), new ST.MapType(new ST.TypeVar("'K"), new ST.TypeVar("'V"))))));
}
}
export class BI_to_list {
constructor() {
this.arity = 1;
this.types = [new ST.MapType()];
this.funTyp =
new ST.PolyFun("'K",
new ST.PolyFun("'V",
new ST.FunType(new ST.MapType(new ST.TypeVar("'K"), new ST.TypeVar("'V")),
new ST.ADT("List", [new ST.ADT("Pair", [new ST.TypeVar("'K"), new ST.TypeVar("'V")])]))));
}
}
export class BI_size {
constructor() {
this.arity = 1;
this.types = [new ST.MapType()];
this.funTyp =
new ST.PolyFun("'K",
new ST.PolyFun("'V",
new ST.FunType(new ST.MapType(new ST.TypeVar("'K"), new ST.TypeVar("'V")),
new ST.Uint32)));
}
}
/**
* Integers
*/
export class BI_lt {
constructor() {
this.arity = 2;
this.types = ST.allInts.concat(ST.allUints);
this.funTyp =
new ST.PolyFun("'A",
new ST.FunType(new ST.TypeVar("'A"),
new ST.FunType(new ST.TypeVar("'A"), new ST.ADT("Bool", []))));
}
}
export class BI_add {
constructor() {
this.arity = 2;
this.types = ST.allInts.concat(ST.allUints);
this.funTyp =
new ST.PolyFun("'A",
new ST.FunType(new ST.TypeVar("'A"),
new ST.FunType(new ST.TypeVar("'A"), new ST.TypeVar("'A"))));
}
}
export class BI_sub {
constructor() {
this.arity = 2;
this.types = ST.allInts.concat(ST.allUints);
this.funTyp =
new ST.PolyFun("'A",
new ST.FunType(new ST.TypeVar("'A"),
new ST.FunType(new ST.TypeVar("'A"), new ST.TypeVar("'A"))));
}
}
export class BI_mul {
constructor() {
this.arity = 2;
this.types = ST.allInts.concat(ST.allUints);
this.funTyp =
new ST.PolyFun("'A",
new ST.FunType(new ST.TypeVar("'A"),
new ST.FunType(new ST.TypeVar("'A"), new ST.TypeVar("'A"))));
}
}
export class BI_div {
constructor() {
this.arity = 2;
this.types = ST.allInts.concat(ST.allUints);
this.funTyp =
new ST.PolyFun("'A",
new ST.FunType(new ST.TypeVar("'A"),
new ST.FunType(new ST.TypeVar("'A"), new ST.TypeVar("'A"))));
}
}
export class BI_rem {
constructor() {
this.arity = 2;
this.types = ST.allInts.concat(ST.allUints);
this.funTyp =
new ST.PolyFun("'A",
new ST.FunType(new ST.TypeVar("'A"),
new ST.FunType(new ST.TypeVar("'A"), new ST.TypeVar("'A"))));
}
}
export class BI_pow {
constructor() {
this.arity = 2;
this.types = ST.allInts.concat(ST.allUints);
this.funTyp =
new ST.PolyFun("'A",
new ST.FunType(new ST.TypeVar("'A"),
new ST.FunType(new ST.Uint32(), new ST.TypeVar("'A"))));
}
}
export class BI_isqrt {
constructor() {
this.arity = 1;
this.types = ST.allUints;
this.funTyp =
new ST.PolyFun("'A",
new ST.FunType(new ST.TypeVar("'A"), new ST.TypeVar("'A")));
}
}
/**
* Signed integers specific builtins (skip)
*/
/**
* Unsigned integers specific builtins (skip)
*/
export class BI_to_uint256 {
constructor() {
this.arity = 1;
this.types = ST.allUints.concat(ST.allBystr.concat([new ST.AnyAddr()]));
this.funTyp =
new ST.PolyFun("'A",
new ST.FunType(new ST.TypeVar("'A"), new ST.Uint256()));
}
}
export class BI_to_uint32 {
constructor() {
this.arity = 1;
this.types = ST.allUints.concat(ST.allBystr.concat([new ST.AnyAddr()])).concat(ST.allInts);
this.funTyp =
new ST.PolyFun("'A",
new ST.FunType(new ST.TypeVar("'A"), new ST.ADT("Option", [new ST.Uint32()])));
}
}
export class BI_badd {
constructor() {
this.arity = 2;
this.types = [new ST.BNum()].concat(ST.allUints);
this.funTyp =
new ST.PolyFun("'A",
new ST.PolyFun("'B",
new ST.FunType(new ST.TypeVar("'A"), new ST.FunType(new ST.TypeVar("'B"), new ST.BNum()))));
}
}
export class BI_sha256hash {
constructor() {
this.arity = 1;
this.types = [new ST.ADT(), new ST.String].concat(ST.allBystr.concat(ST.allAddr)).concat(ST.allInts);
this.funTyp =
new ST.PolyFun("'A",
new ST.FunType(new ST.TypeVar("'A"), new ST.ByStrXTyp(32))
);
}
}
/**
* Recursive Functions
*/
export class BI_list_foldl {
constructor() {
this.arity = 3;
this.types = [new ST.String()].concat(ST.allUints.concat(ST.allInts.concat(ST.allAddr.concat(ST.allBystr))))
this.funTyp =
new ST.PolyFun("'A",
new ST.PolyFun("'B",
new ST.FunType(
new ST.FunType(new ST.TypeVar("'B"), new ST.FunType(new ST.TypeVar("'A"), new ST.TypeVar("'B"))),
new ST.FunType(new ST.TypeVar("'B"),
new ST.FunType(new ST.ADT("List", [new ST.TypeVar("'A")]),
new ST.TypeVar("'B"))))));
//('B -> 'A -> 'B) -> 'B -> (List 'A) -> 'B
}
}
export class BI_list_foldr {
constructor() {
this.arity = 3;
this.types = [new ST.String()].concat(ST.allUints.concat(ST.allInts.concat(ST.allAddr.concat(ST.allBystr))))
this.funTyp =
new ST.PolyFun("'A",
new ST.PolyFun("'B",
new ST.FunType(
new ST.FunType(new ST.TypeVar("'A"), new ST.FunType(new ST.TypeVar("'B"), new ST.TypeVar("'B"))),
new ST.FunType(new ST.TypeVar("'B"),
new ST.FunType(new ST.ADT("List", [new ST.TypeVar("'A")]),
new ST.TypeVar("'B"))))));
//('A -> 'B -> 'B) -> 'B -> (List 'A) -> 'B
}
}
export class BI_list_foldk {
constructor() {
this.arity = 3;
this.types = [new ST.String()].concat(ST.allUints.concat(ST.allInts.concat(ST.allAddr.concat(ST.allBystr))))
this.funTyp =
new ST.PolyFun("'A",
new ST.PolyFun("'B",
new ST.FunType(
//('B -> 'A -> ('B -> 'B) -> 'B)
new ST.FunType(
new ST.TypeVar("'B"),
new ST.FunType(new ST.TypeVar("'A"),
new ST.FunType(new ST.FunType(new ST.TypeVar("'B"), new ST.TypeVar("'B")),
new ST.TypeVar("'B")))),
new ST.FunType(new ST.TypeVar("'B"), new ST.FunType(new ST.ADT("List", [new ST.TypeVar("'A")]), new ST.TypeVar("'B")))
)));
//('B -> 'A -> ('B -> 'B) -> 'B) -> 'B -> (List 'A) -> 'B
}
}
export class BI_nat_fold {
constructor() {
this.arity = 3;
this.types = [new ST.String()].concat(ST.allUints.concat(ST.allInts.concat(ST.allAddr.concat(ST.allBystr))));
this.funTyp =
new ST.PolyFun("'T",
new ST.FunType(
new ST.FunType(new ST.TypeVar("'T"), new ST.FunType(new ST.ADT("Nat", []), new ST.TypeVar("'T"))),
new ST.FunType(new ST.TypeVar("'T"),
new ST.FunType(new ST.ADT("Nat", []), new ST.TypeVar("'T")))
));
//('T -> Nat -> 'T) -> 'T -> Nat -> 'T
}
}
export class BI_nat_foldk {
constructor() {
this.arity = 3;
this.types = [new ST.String()].concat(ST.allUints.concat(ST.allInts.concat(ST.allAddr.concat(ST.allBystr))));
this.funTyp =
new ST.PolyFun("'T",
new ST.FunType(
new ST.FunType(
new ST.TypeVar("'T"),
new ST.FunType(
new ST.ADT("Nat", []),
new ST.FunType(
new ST.FunType(new ST.TypeVar("'T"), new ST.TypeVar("'T")),
new ST.TypeVar("'T")
)
)
),
new ST.FunType(
new ST.TypeVar("'T"),
new ST.FunType(new ST.ADT("Nat", []), new ST.TypeVar("'T")))
)
);
//('T -> Nat -> ('T -> 'T) -> 'T) -> 'T -> Nat -> 'T
}
}
export class BI_to_nat{
constructor() {
this.arity = 1;
this.types = [new ST.Uint32];
this.funTyp = new ST.FunType(new ST.Uint32, new ST.ADT("Nat", []));
}
}
export const BuiltInDict = {
"eq": new BI_eq(),
"concat": new BI_concat(),
"substr": new BI_substr(),
"strlen": new BI_strlen(),
"to_string": new BI_to_string(),
"to_ascii": new BI_to_ascii(),
"strrev": new BI_strrev(),
"to_bystrx": new BI_to_bystrx(),
"contains": new BI_contains(),
"put": new BI_put(),
"get": new BI_get(),
"remove": new BI_remove(),
"to_list": new BI_to_list(),
"size": new BI_size(),
"lt": new BI_lt(),
"add": new BI_add(),
"sub": new BI_sub(),
"mul": new BI_mul(),
"lt": new BI_lt(),
"div": new BI_div(),
"rem": new BI_rem(),
"pow": new BI_pow(),
"isqrt": new BI_isqrt(),
"blt": new BI_blt(),
"list_foldl": new BI_list_foldl(),
"list_foldr": new BI_list_foldr(),
"list_foldk": new BI_list_foldk(),
"nat_fold": new BI_nat_fold(),
"to_nat": new BI_to_nat(),
"to_bystr": new BI_to_bystr(),
"to_uint256": new BI_to_uint256(),
"to_uint32": new BI_to_uint32(),
"badd": new BI_badd(),
"nat_foldk": new BI_nat_foldk(),
"sha256hash": new BI_sha256hash()
}
/**
*
* @param {String} fname - Builtin function name
* @param {SType[]} targs - Types of arguments
* Returns: FunType or an Error
*/
//Note: Function also type checks - making sure targs are valid
export function resolveBIFunType(fname, targs) {
function checkBasics(info) {
if (targs.length !== info.arity) {
return ER.setError(new Error("resolveBIFunType: Wrong arity for function."));
}
//Update: We only look at the first targ and if it's allowed
//Eg. contains allows only type Map - but we would also have a type like Int32 of what the map contains.
if (info.types.find(ty => ty.constructor === targs[0].constructor) === undefined) {
return ER.setError(new Error("resolveBIFunType: Type of arguments are not allowed to this function."));
} else {
return true;
}
}
if (fname === "eq") {
const info = BuiltInDict[fname];
const basicsOk = checkBasics(info);
if (ER.isError()) { return;};
const fType = info.funTyp;
//All address types are compared as bystr
if (targs[0] instanceof ST.AddressType) {
targs[0] = new ST.ByStrXTyp(20);
}
return ST.substTypeinType(fType.name, targs[0], fType.t);
}
if (fname === "badd") {
const info = BuiltInDict[fname];
const basicsOk = checkBasics(info);
if (ER.isError()) { return; }
const fType = info.funTyp;
//Instantiate both 'A and 'B
const ty1 = ST.substTypeinType(fType.name, targs[0], fType.t);
const ty2 = ST.substTypeinType(ty1.name, targs[1], ty1.t);
return ty2;
}
if (fname === "concat" || fname === "substr" ||
fname === "to_string" || fname === "to_ascii" || fname === "lt" ||
fname === "add" || fname === "sub" || fname === "mul" || fname === "div" ||
fname === "rem" || fname === "pow" || fname === "isqrt" ||
fname === "strlen" || fname === "strrev" || fname === "to_bystrx" || fname === "to_uint256" ||
fname === "to_uint32" || fname === "sha256hash")
{
const info = BuiltInDict[fname];
const basicsOk = checkBasics(info);
if (ER.isError()) { return;};
const fType = info.funTyp;
return ST.substTypeinType(fType.name, targs[0], fType.t);
}
//Map builtins
if (fname === "contains" || fname === "put" || fname === "get" ||
fname === "remove" || fname === "to_list" || fname === "size")
{
const info = BuiltInDict[fname];
const basicsOk = checkBasics(info);
if (ER.isError()) { return; };
const fType = info.funTyp;
const mapArg = targs[0]; //Map argument is always the first
//Substitute t1 (key type)
const fType_ = ST.substTypeinType(fType.name, mapArg.t1, fType.t);
//Substitute t2 (value type)
const fType__ = ST.substTypeinType(fType_.name, mapArg.t2, fType_.t);
return fType__;
}
//Other builtins
if (BuiltInDict[fname]) {
return BuiltInDict[fname].funTyp;
}
}
export function resolveBITargs(fname, targs) {
if (fname === "eq") {
//Arity is already checked in resolveBIFunType
targs.map(targ => {
if (targ instanceof ST.AddressType) {
return new ST.ByStrXTyp(20);
}
return targ;
})
return targs;
}
return targs;
}