-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathdimplify
executable file
·130 lines (98 loc) · 3.61 KB
/
dimplify
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
#!/usr/bin/env node
/* global require, process, fs */
var tokenize = require('./tokenize.js').tokenize;
var church_astify = require('./church_astify.js').church_astify;
var js_astify = require('./js_astify.js').church_tree_to_esprima_ast;
//var precompile = require('./precompile.js').precompile;
var wctransform = require('./wctransform');
var escodegen = require('escodegen');
var esprima = require('esprima');
var estraverse = require('escodegen/node_modules/estraverse');
var trace = require('./trace.js');
var ttd = require('./trace-to-dimple.js');
var fs = require('fs');
var logStr = "";
var log = function(x) {
logStr += x + "\n";
}
function abort(msg) {
console.log(msg)
process.exit(0);
}
var filename = process.argv.length > 2 ? process.argv[process.argv.length-1] : abort("Usage: dimplify [OPTIONS]... [FILE]");
process.argv = process.argv.slice(0, -1);
var optparse = require('optparse');
var switches = [
['-q', '--quiet', "Don't emit code (only shows errors and warning)"] ,
['-t', '--traced', "Show traced JS"]
];
var parser = new optparse.OptionParser(switches);
var opts = {
quiet: false,
traced: false,
dimplified: true
};
parser.on('quiet', function() { opts.quiet = true; });
parser.on('traced', function() { opts.traced = true; });
parser.parse(process.argv);
var church_codestring = fs.readFileSync(filename);
////Ising example:
//(define sites (repeat 10 flip))
//(define (constraints s) (if (null? s) )
//preamble is an array of strings defining church functions for the precompile pass. these all overload built in functions from the church or probjs runtime.
var preamble = []
//ERPs have to be intercepted and overloaded with a form that calls "random" to make an abstract value.
//var erps = ["uniform-draw", "multinomial", "flip", "uniform", "random_integer", "gaussian", "gamma", "beta", "dirichlet"]
var erps = ["wrapped_uniform_draw", "wrapped_multinomial", "wrapped_flip", "wrapped_uniform", "wrapped_random_integer", "wrapped_gaussian", "wrapped_gamma", "wrapped_beta", "wrapped_dirichlet"]
for (var p in erps) {
// preamble.push("(define "+ erps[p] +" (lambda args (random '"+ erps[p] +" args idstack)))")
preamble.push("(define "+ erps[p] +" (lambda args (random '"+ erps[p] +" args)))")
}
//console.log(preamble.join('\n'))
church_codestring = preamble.join("\n")+ "\n" + church_codestring
var tokens = tokenize(church_codestring);
var church_ast = church_astify(tokens);
var js_ast = js_astify(church_ast);
js_ast = wctransform.probTransformAST(js_ast, true);
// console.log('wctransformed code')
// console.log('------------------\n')
// console.log(escodegen.generate(js_ast))
// console.log('')
//trace:
var tracecode = trace.trace(js_ast);
if (opts.traced) {
console.log('traced code')
console.log('-----------\n')
console.log(tracecode)
console.log('')
}
// to dimple:
console.log('dimple code')
console.log('-----------\n')
var javaImports = [
"cern.colt.Arrays",
"com.analog.lyric.dimple.factorfunctions.*",
"com.analog.lyric.dimple.model.variables.*",
"com.analog.lyric.dimple.model.domains.*",
"com.analog.lyric.dimple.model.core.*",
"com.analog.lyric.dimple.solvers.gibbs.*;"
];
log(javaImports.map(function(imp) {
return "import " + imp + ";";
}).join("\n"));
log("");
log("public class Simple")
log("{")
log("public static void main(String[] args)");
log("{")
log("")
// use gibbs
log("FactorGraph fg = new FactorGraph();");
log("SFactorGraph solver = fg.setSolverFactory(new GibbsSolver());");
log("solver.setNumSamples(5);");
log(ttd.traceToDimple(tracecode))
log("}")
log("}")
if (!opts.quiet) {
console.log(logStr)
}