-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtraced_builtins.js
79 lines (62 loc) · 2.29 KB
/
traced_builtins.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
// generates a module with traced versions of the church builtins.
_ = require("underscore");
shred = require("./shred");
church = require("./webchurch/church_builtins");
bt = require("./builtin_transformations.js");
// Begin module generation======================================================
var untraced_primitives = [
"iota", "make_list", "repeat", "map", "for_each", "reverse", // more constructive
"filter", "take", "length", "sum", // more destructive
"append", "flatten", // construct/destruct lists of lists
"fold" // everything
];
module.exports.untraced_primitives = untraced_primitives;
synth_builtins = bt.synthesize_builtins(church, shred.shred, untraced_primitives);
module.exports = synth_builtins.exports;
module.exports.__annotations__ = synth_builtins.exports.__annotations__;
// Important stuff
module.exports._const = shred._const;
module.exports._if = shred._if;
module.exports.dump_trace = shred.dump_trace
// Patch things up
var _is_null = module.exports["is_null"];
var _pair = module.exports["pair"];
var _first = module.exports["first"];
var _rest = module.exports["rest"];
var recMap = function(f, xs) {
return shred._if(_is_null(xs),
function () { return shred._const([null]); },
function () { return _pair(f(_first(xs)), recMap(f, _rest(xs))); });
}
var recSum = function(xs) {
return shred._if(_is_null(xs),
function () { return _const(0); },
function () { return module.exports.plus(_first(xs),recSum(_rest(xs))); });
}
// TODO: rest of the untraced primitives
replaced_functions = {
map : {
name : 'map',
desc : 'traced map',
params : [{name : 'f'}, {name : 'xs'}],
fn : recMap
},
sum : {
name : 'sum',
desc : 'traced sum',
params : [{name : 'xs'}],
fn : recSum
}
}
// patch the untraced primitives back in
for (idx in untraced_primitives) {
name = untraced_primitives[idx];
if (!(name in module.exports.__annotations__)) {
if (name in replaced_functions) {
bt.addBuiltin(module, replaced_functions[name])
} else {
orig_dict = church.__annotations__[name];
bt.addBuiltin(module, orig_dict);
}
}
}