Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
stuhlmueller committed Oct 13, 2015
2 parents acbfc03 + eb6fe2f commit ddc625b
Show file tree
Hide file tree
Showing 22 changed files with 371 additions and 132 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ node_js:
- "0.12"
- "0.11"
- "0.10"
- "4.0.0"
- "4"

install:
- npm install
Expand Down
6 changes: 5 additions & 1 deletion compiled/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,18 @@ SRCFILES = $(wildcard ../src/*.js) \
MAINFILE = ../src/browser.js
MINIFIED = webppl.min.js
BROWSERIFIED = webppl.js
TESTS = ../tests/browser/index.html

all: $(MINIFIED)

$(MINIFIED): $(BROWSERIFIED)
uglifyjs $< -b ascii_only=true,beautify=false > $@

$(BROWSERIFIED): $(SRCFILES)
browserify -t brfs $(MAINFILE) > $@
browserify -g brfs $(MAINFILE) > $@

test: $(BROWSERIFIED)
node -e "require('open')(process.argv[1], process.env.BROWSER)" $(TESTS)

clean:
rm -f $(MINIFIED) $(BROWSERIFIED)
2 changes: 1 addition & 1 deletion docs/analysis/documentation.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
HOW TO USE IT

The top level of the control-flow analysis is the 'analyze' function in the 'analysis/main' module. This function takes a named, CPS'd AST (which 'prepare' of the 'main' module produces given a literal program) and produces a relation over abstract program states along with some information about the relation itself. All this is encoded as a JS object naming this information. These parts are
The top level of the control-flow analysis is the 'analyze' function in the 'analysis/main' module. This function takes a named, CPS'd AST (which 'prepare' of the 'analysis/main' module produces given a literal program) and produces a relation over abstract program states along with some information about the relation itself. All this is encoded as a JS object naming this information. These parts are

- 'seen': the set of abstract states over which the relation is defined
- 'calls': a map from an abstract entry point to a set of abstract call points
Expand Down
17 changes: 14 additions & 3 deletions docs/development/workflow.rst
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,24 @@ many of them automatically using::

To compile webppl for use in browser, run::

npm install -g browserify
browserify -t brfs src/browser.js > compiled/webppl.js
npm install -g browserify uglifyjs
cd compiled
make clean
make

Then, to run the browser tests use::

make test

The tests will run in the default browser. Specify a different browser
using the ``BROWSER`` environment variable. For example::

BROWSER="Google Chrome" make test

Packages can also be used in the browser. For example, to include the
``webppl-viz`` package use::

browserify -t [./src/bundle.js --require webppl-viz] -t brfs src/browser.js > compiled/webppl.js
browserify -t [./src/bundle.js --require webppl-viz] -g brfs src/browser.js > compiled/webppl.js

Multiple ``--require`` arguments can be used to include multiple
packages.
Expand Down
34 changes: 32 additions & 2 deletions docs/packages.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ WebPPL Packages
===============

WebPPL packages are regular Node.js packages optionally extended to
include WebPPL code and headers.
include WebPPL code, macros and headers.

To make a package available in your program use the ``--require``
argument::
Expand All @@ -21,7 +21,7 @@ Packages can be loaded from other locations by passing a path::

webppl myFile.wppl --require ../myPackage

Packages can extend WebPPL in three ways:
Packages can extend WebPPL in several ways:

WebPPL code
-----------
Expand All @@ -36,6 +36,33 @@ You can automatically prepend WebPPL files to your code by added a
}
}

Macros
------

`sweet.js`_ modules can be included in a package as follows:

1. Add a file containing the macros to the package::

// macros.sjs
macro m { /* ... */ }
export m;

Note that macros must be exported explicitly using the ``export``
keyword. See the `sweet.js module documentation`_ for further details.

2. Add a ``macros`` entry to ``package.json``::

{
"name": "my-package",
"webppl": {
"macros": ["macros.sjs"]
}
}

These macros will be visible to the WebPPL program which is been run
or compiled, and to any WebPPL code within the same package. They will
not be visible to WebPPL code in other packages.

Javascript functions and libraries
----------------------------------

Expand Down Expand Up @@ -118,3 +145,6 @@ available in WebPPL:
};

foo();

.. _sweet.js: http://sweetjs.org
.. _sweet.js module documentation: http://sweetjs.org/doc/main/sweet.html#using-modules
2 changes: 1 addition & 1 deletion docs/usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Running webppl programs::

Seeding the random number generator::

RANDOM_SEED=2344512342 webppl examples/lda.wppl
webppl examples/lda.wppl --random-seed 2344512342

Compiling webppl programs to Javascript::

Expand Down
8 changes: 5 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "webppl",
"description": "Probabilistic programming for the web",
"version": "0.2.1",
"version": "0.3.0",
"author": "webppl contributors",
"main": "src/main.js",
"repository": {
Expand All @@ -24,18 +24,20 @@
"git-rev-2": "^0.1.0",
"immutable": "^3.7.1",
"minimist": "^1.1.1",
"priorityqueuejs": "~1.0.0",
"numeric": "~1.2.6",
"priorityqueuejs": "~1.0.0",
"sweet.js": "probmods/sweet.js#browserify",
"underscore": "^1.8.3"
},
"devDependencies": {
"brfs": "^1.4.0",
"brfs": "0.0.8",
"closure-linter-wrapper": "^0.2.11",
"grunt": "^0.4.5",
"grunt-contrib-jshint": "^0.11.1",
"grunt-contrib-nodeunit": "^0.4.1",
"grunt-gjslint": "^0.1.6",
"nodeunit": "^0.9.1",
"open": "0.0.5",
"seedrandom": "^2.4.2",
"through2": "^2.0.0"
},
Expand Down
17 changes: 16 additions & 1 deletion src/analysis/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ var parse = require('./parser-combinator');
var analyzeRefs = require('./analyze-refs').analyzeRefs;
var isHeapRef = require('./analyze-refs').isHeapRef;

var thunkify = require('../syntax').thunkify;
var naming = require('../transforms/naming').naming;
var cps = require('../transforms/cps').cps;
var optimize = require('../transforms/optimize').optimize;

var compile = require('../main').compile;

var isHeapVar = null;

Expand Down Expand Up @@ -478,6 +484,15 @@ function analyzeMain(program) {
}
}

function prepare(code, verbose) {
return compile(code, {
transforms: [thunkify, naming, cps, optimize],
verbose: verbose,
generateCode: false
});
}

module.exports = {
analyze: analyzeMain
analyze: analyzeMain,
prepare: prepare
};
4 changes: 2 additions & 2 deletions src/analysis/visualize-run.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';

var prepare = require('../main').prepare;
var analyze = require('../main').analyze;
var prepare = require('./main').prepare;
var analyze = require('./main').analyze;
var vizualize = require('./visualize').vizualize;

process.stdin.setEncoding('utf8');
Expand Down
9 changes: 4 additions & 5 deletions src/browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
var fs = require('fs');
var esprima = require('esprima');
var escodegen = require('escodegen');

var webppl = require('./main');
var optimize = require('./transforms/optimize').optimize;
var naming = require('./transforms/naming').naming;
Expand All @@ -23,14 +22,14 @@ packages.forEach(function(pkg) {
pkg.headers.forEach(webppl.requireHeaderWrapper);
});

var wpplExtra = packages.map(function(pkg) { return pkg.wppl.join(';'); }).join(';');

function run(code, k, verbose) {
return webppl.run(wpplExtra + code, k, verbose);
var extra = webppl.parsePackageCode(packages, verbose);
return webppl.run(code, k, { extra: extra, verbose: verbose });
}

function compile(code, verbose) {
return webppl.compile(wpplExtra + code, verbose);
var extra = webppl.parsePackageCode(packages, verbose);
return webppl.compile(code, { extra: extra, verbose: verbose });
}

function webpplCPS(code) {
Expand Down
2 changes: 2 additions & 0 deletions src/headerMacros.sjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
operator (|>) 0 left { $val, $f } => #{ $f($val) }
export (|>)
12 changes: 7 additions & 5 deletions src/inference/incrementalmh.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,13 @@ module.exports = function(env) {
tabbedlog(4, this.depth, 'no, ERP params have not changed');
tabbedlog(5, this.depth, 'params:', this.params);
}
return this.kontinue();
// Bail out early if we know proposal will be rejected
if (this.score === -Infinity) {
tabbedlog(4, this.depth, 'score became -Infinity; bailing out early');
return this.coroutine.exit();
} else {
return this.kontinue();
}
};

ERPNode.prototype.registerInputChanges = function(s, k, unused, params) {
Expand Down Expand Up @@ -178,10 +184,6 @@ module.exports = function(env) {
var oldscore = this.score;
updateProperty(this, 'score', this.erp.score(this.params, this.val));
this.coroutine.score += this.score - oldscore;
if (this.score === -Infinity) {
tabbedlog(4, this.depth, 'score became -Infinity; bailing out early');
return this.coroutine.exit();
}
};

// ------------------------------------------------------------------
Expand Down

0 comments on commit ddc625b

Please sign in to comment.