goodguide-gibbon 0.4.2 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -382,6 +382,8 @@ genId = (function() {
382
382
 
383
383
 
384
384
 
385
+
386
+
385
387
  isArray = Array.isArray || function(arg) {
386
388
  return Object.prototype.toString.call(arg) === '[object Array]';
387
389
  };
@@ -442,8 +444,9 @@ asyncMap = function(list, mapper, cb) {
442
444
  seen += 1;
443
445
  output[i] = el;
444
446
  if (seen >= list.length) {
445
- return cb(output);
447
+ cb(output);
446
448
  }
449
+ return null;
447
450
  };
448
451
  };
449
452
  _results = [];
@@ -699,6 +702,8 @@ Gibbon.Hash = Hash = (function(_super) {
699
702
  return _ref;
700
703
  }
701
704
 
705
+ Hash.prototype.__isHash__ = true;
706
+
702
707
  salt = '<key>';
703
708
 
704
709
  Hash.prototype.loadKey = function(k) {
@@ -1281,7 +1286,7 @@ Gibbon.Semantic = Semantic = (function(_super) {
1281
1286
  block: ['body'],
1282
1287
  list: ['elements', 'squish'],
1283
1288
  flow: ['type', 'head', 'tail'],
1284
- func: ['name', 'args'],
1289
+ func: ['name', 'args', 'scope'],
1285
1290
  subst: ['flow'],
1286
1291
  defaulted: ['body', 'alternative']
1287
1292
  });
@@ -1716,6 +1721,7 @@ analyze = Gibbon.analyze = (function() {
1716
1721
  func = stdlib[name];
1717
1722
  ast = func.type;
1718
1723
  scope = new Hash;
1724
+ flow.head.__scope__ = scope;
1719
1725
  input = TypeExpr.fromAST(ast.input, scope);
1720
1726
  if (flow.tail) {
1721
1727
  push(TypeExpr.expr(flow.tail), input);
@@ -1941,13 +1947,13 @@ analyze = Gibbon.analyze = (function() {
1941
1947
  done = function() {
1942
1948
  var flowType, toSemanticTree;
1943
1949
  flowType = function(expr) {
1944
- if (!solutions.has(TypeExpr.expr(expr))) {
1950
+ if (!solutions.has(expr)) {
1945
1951
  if (errors.length === 0) {
1946
1952
  throw new Error('unsolved!');
1947
1953
  }
1948
1954
  return Type.abstract(TypeExpr.expr(expr));
1949
1955
  }
1950
- return solutions.get(TypeExpr.expr(expr)).realize();
1956
+ return solutions.get(expr).realize();
1951
1957
  };
1952
1958
  toSemanticTree = function(expr) {
1953
1959
  return expr.cases({
@@ -1955,7 +1961,7 @@ analyze = Gibbon.analyze = (function() {
1955
1961
  return Semantic.definition(dependencies, toSemanticTree(flow));
1956
1962
  },
1957
1963
  flow: function(head, tail) {
1958
- return Semantic.flow(flowType(this), toSemanticTree(head), tail && toSemanticTree(tail));
1964
+ return Semantic.flow(flowType(TypeExpr.expr(this)), toSemanticTree(head), tail && toSemanticTree(tail));
1959
1965
  },
1960
1966
  query: function(type, name) {
1961
1967
  var _this = this;
@@ -1963,8 +1969,13 @@ analyze = Gibbon.analyze = (function() {
1963
1969
  return semanticAccessors.get(this);
1964
1970
  },
1965
1971
  func: function(name, args) {
1966
- var a;
1967
- return Semantic.func(name, (function() {
1972
+ var a, scope, semArgs, solvedScope;
1973
+ scope = this.__scope__;
1974
+ solvedScope = new Hash;
1975
+ scope && scope.each(function(name, texpr) {
1976
+ return solvedScope.set(name, flowType(texpr));
1977
+ });
1978
+ semArgs = (function() {
1968
1979
  var _i, _len, _results;
1969
1980
  _results = [];
1970
1981
  for (_i = 0, _len = args.length; _i < _len; _i++) {
@@ -1972,7 +1983,8 @@ analyze = Gibbon.analyze = (function() {
1972
1983
  _results.push(toSemanticTree(a));
1973
1984
  }
1974
1985
  return _results;
1975
- })());
1986
+ })();
1987
+ return Semantic.func(name, semArgs, solvedScope);
1976
1988
  },
1977
1989
  pair: function(first, second) {
1978
1990
  return Semantic.pair(toSemanticTree(first), toSemanticTree(second));
@@ -2145,9 +2157,6 @@ analyze = Gibbon.analyze = (function() {
2145
2157
  })();
2146
2158
  return function(program, globalID, external, cb) {
2147
2159
  var constraints;
2148
- if (!(program instanceof AST)) {
2149
- program = AST.fromJSON(program);
2150
- }
2151
2160
 
2152
2161
 
2153
2162
  constraints = generate(globalID, external.analyzeQuery, program);
@@ -2155,14 +2164,11 @@ analyze = Gibbon.analyze = (function() {
2155
2164
  };
2156
2165
  })();
2157
2166
  // Generated by CoffeeScript 1.6.3
2158
- var Dependency, Failure, Promise, Value, eval_, _ref, _ref1, _ref2,
2167
+ var CachingPromise, Dependency, Failure, Promise, Thunk, UnitPromise, Value, eval_, _ref, _ref1, _ref2, _ref3,
2159
2168
  __hasProp = {}.hasOwnProperty,
2160
- __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; },
2161
- __slice = [].slice;
2169
+ __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; };
2162
2170
 
2163
2171
  Value = Gibbon.Value = Value = (function(_super) {
2164
- var thunksAreEqual;
2165
-
2166
2172
  __extends(Value, _super);
2167
2173
 
2168
2174
  function Value() {
@@ -2217,37 +2223,6 @@ Value = Gibbon.Value = Value = (function(_super) {
2217
2223
  }));
2218
2224
  };
2219
2225
 
2220
- thunksAreEqual = function(list1, list2) {
2221
- var i, out, step, x, zipped;
2222
- if (list1.length !== list2.length) {
2223
- return Promise.unit(Value.boolean(false));
2224
- }
2225
- zipped = (function() {
2226
- var _i, _len, _results;
2227
- _results = [];
2228
- for (i = _i = 0, _len = list1.length; _i < _len; i = ++_i) {
2229
- x = list1[i];
2230
- _results.push(Promise.combine(x, list2[i]));
2231
- }
2232
- return _results;
2233
- })();
2234
- step = function(_arg, next) {
2235
- var x, y;
2236
- x = _arg[0], y = _arg[1];
2237
- return x.equals(y).then(function(isEqual) {
2238
- if (isEqual.value) {
2239
- return next();
2240
- } else {
2241
- return Promise.unit(Value.boolean(false));
2242
- }
2243
- });
2244
- };
2245
- out = function() {
2246
- return Promise.unit(Value.boolean(true));
2247
- };
2248
- return Promise.iter(zipped, step, out);
2249
- };
2250
-
2251
2226
  Value.prototype.equals = function(other) {
2252
2227
  var wrap;
2253
2228
  wrap = function(b) {
@@ -2298,26 +2273,6 @@ Value = Gibbon.Value = Value = (function(_super) {
2298
2273
  });
2299
2274
  };
2300
2275
 
2301
- Value.prototype.mapAsync = function(f, cb) {
2302
- return this.cases({
2303
- list: function(els) {
2304
- return asyncMap(els, f, function(mapped) {
2305
- return Value.list(mapped);
2306
- });
2307
- },
2308
- pair: function(first, second) {
2309
- return asyncMap([first, second], f, function(_arg) {
2310
- var first, second;
2311
- first = _arg[0], second = _arg[1];
2312
- return Value.pair(first, second);
2313
- });
2314
- },
2315
- other: function() {
2316
- return cb(this);
2317
- }
2318
- });
2319
- };
2320
-
2321
2276
  Value.prototype.mapPromise = function(f) {
2322
2277
  return this.cases({
2323
2278
  list: function(promises) {
@@ -2363,16 +2318,47 @@ Value = Gibbon.Value = Value = (function(_super) {
2363
2318
 
2364
2319
  })(Union);
2365
2320
 
2321
+ Thunk = (function() {
2322
+ Thunk.trampoline = function(t) {
2323
+ while (t instanceof Thunk) {
2324
+ t = t.run();
2325
+ }
2326
+ return t;
2327
+ };
2328
+
2329
+ function Thunk(run) {
2330
+ this.run = run;
2331
+ }
2332
+
2333
+ return Thunk;
2334
+
2335
+ })();
2336
+
2366
2337
  Promise = (function() {
2367
- function Promise(force) {
2368
- this.force = force;
2338
+ function Promise(forceRaw) {
2339
+ this.forceRaw = forceRaw;
2369
2340
  }
2370
2341
 
2342
+ Promise.prototype.force = function(fail, cc) {
2343
+ return Thunk.trampoline(this.forceRaw(fail, cc));
2344
+ };
2345
+
2346
+ Promise.prototype.step = function(fail, cc) {
2347
+ var _this = this;
2348
+ return new Thunk(function() {
2349
+ return _this.forceRaw(fail, cc);
2350
+ });
2351
+ };
2352
+
2371
2353
  Promise.prototype.then = function(fn) {
2372
2354
  var _this = this;
2373
2355
  return new Promise(function(fail, cb) {
2374
- return _this.force(fail, function(val, deps) {
2375
- return fn(val, deps).depends(deps).force(fail, cb);
2356
+ return _this.step(fail, function(val, deps) {
2357
+ var next;
2358
+ next = fn(val, deps).depends(deps);
2359
+ return new Thunk(function() {
2360
+ return next.forceRaw(fail, cb);
2361
+ });
2376
2362
  });
2377
2363
  });
2378
2364
  };
@@ -2382,9 +2368,11 @@ Promise = (function() {
2382
2368
  return new Promise(function(fail, cb) {
2383
2369
  var rescue;
2384
2370
  rescue = function(failure) {
2385
- return fn(failure).depends([Dependency.failure(failure)]).force(fail, cb);
2371
+ var rescuer;
2372
+ rescuer = fn(failure).depends([Dependency.failure(failure)]);
2373
+ return rescuer.step(fail, cb);
2386
2374
  };
2387
- return _this.force(rescue, cb);
2375
+ return _this.step(rescue, cb);
2388
2376
  });
2389
2377
  };
2390
2378
 
@@ -2394,67 +2382,95 @@ Promise = (function() {
2394
2382
  });
2395
2383
  };
2396
2384
 
2397
- Promise.prototype.depends = function(deps) {
2385
+ Promise.prototype.mapDeps = function(f) {
2398
2386
  var _this = this;
2399
2387
  return new Promise(function(fail, cc) {
2400
- return _this.force(fail, function(val, prevDeps) {
2401
- return cc(val, prevDeps.concat(deps));
2388
+ return _this.step(fail, function(val, deps) {
2389
+ return new Thunk(function() {
2390
+ return cc(val, f(deps));
2391
+ });
2402
2392
  });
2403
2393
  });
2404
2394
  };
2405
2395
 
2406
- Promise.prototype.map = function(f) {
2407
- return this.then(function(v, d) {
2408
- return Promise.unit(f(v, d));
2396
+ Promise.prototype.depends = function(deps) {
2397
+ if (deps.length === 0) {
2398
+ return this;
2399
+ }
2400
+ return this.mapDeps(function(prevDeps) {
2401
+ return prevDeps.concat(deps);
2409
2402
  });
2410
2403
  };
2411
2404
 
2412
- Promise.prototype.mapDependencies = function(f) {
2413
- var _this = this;
2414
- return new Promise(function(fail, cc) {
2415
- return _this.force(fail, function(val, deps) {
2416
- return cc(val, f(deps));
2417
- });
2405
+ Promise.prototype.dependsOnly = function(deps) {
2406
+ return this.mapDeps(function() {
2407
+ return deps;
2408
+ });
2409
+ };
2410
+
2411
+ Promise.prototype.map = function(f) {
2412
+ return this.then(function(v, d) {
2413
+ return Promise.unit(f(v, d));
2418
2414
  });
2419
2415
  };
2420
2416
 
2421
2417
  Promise.prototype.after = function(f) {
2422
2418
  var _this = this;
2423
2419
  return new Promise(function(fail, cc) {
2424
- return _this.force(fail, function(val, deps) {
2420
+ return _this.step(fail, function(val, deps) {
2425
2421
  f(val, deps);
2426
- return cc(val, deps);
2422
+ return new Thunk(function() {
2423
+ return cc(val, deps);
2424
+ });
2427
2425
  });
2428
2426
  });
2429
2427
  };
2430
2428
 
2431
2429
  Promise.unit = function(e) {
2432
- return new Promise(function(_, f) {
2433
- return f(e, []);
2430
+ return new UnitPromise(function(_, cc) {
2431
+ return new Thunk(function() {
2432
+ return cc(e, []);
2433
+ });
2434
2434
  });
2435
2435
  };
2436
2436
 
2437
2437
  Promise.fail = function(fail) {
2438
2438
  return new Promise(function(f, _) {
2439
- return f(fail);
2439
+ return new Thunk(function() {
2440
+ return f(fail);
2441
+ });
2440
2442
  });
2441
2443
  };
2442
2444
 
2443
2445
  Promise.lazy = function(f) {
2444
2446
  return new Promise(function(fail, cc) {
2445
- return f().force(fail, cc);
2447
+ return f().step(fail, cc);
2446
2448
  });
2447
2449
  };
2448
2450
 
2449
- Promise.combine = function(thunks) {
2450
- return new Promise(function(fail, cb) {
2451
- var mapper;
2452
- mapper = function(thunk, push) {
2453
- return thunk.force(fail, function(val, deps) {
2451
+ Promise.prototype.cache = function() {
2452
+ return new CachingPromise(this.forceRaw);
2453
+ };
2454
+
2455
+ Promise.combine = function(promises) {
2456
+ if (promises.length === 0) {
2457
+ return Promise.unit([]);
2458
+ }
2459
+ if (promises.length === 1) {
2460
+ return promises[0].map(function(t) {
2461
+ return [t];
2462
+ });
2463
+ }
2464
+ return new Promise(function(fail, cc) {
2465
+ var isSynchronous, mapper, outThunk;
2466
+ isSynchronous = true;
2467
+ outThunk = null;
2468
+ mapper = function(promise, push) {
2469
+ return promise.force(fail, function(val, deps) {
2454
2470
  return push([val, deps]);
2455
2471
  });
2456
2472
  };
2457
- return asyncMap(thunks, mapper, function(vals) {
2473
+ asyncMap(promises, mapper, function(vals) {
2458
2474
  var outDeps, outVal, v;
2459
2475
  outVal = (function() {
2460
2476
  var _i, _len, _results;
@@ -2474,41 +2490,86 @@ Promise = (function() {
2474
2490
  }
2475
2491
  return _results;
2476
2492
  })());
2477
- return cb(outVal, outDeps);
2493
+ if (isSynchronous) {
2494
+ return outThunk = new Thunk(function() {
2495
+ return cc(outVal, outDeps);
2496
+ });
2497
+ } else {
2498
+ return Thunk.trampoline(cc(outVal, outDeps));
2499
+ }
2478
2500
  });
2501
+ isSynchronous = false;
2502
+ return outThunk;
2479
2503
  });
2480
2504
  };
2481
2505
 
2482
- Promise.iter = function(list, step, out) {
2483
- var _loop;
2484
- _loop = function(i) {
2485
- if (i >= list.length) {
2486
- return out();
2487
- }
2488
- return list[i].then(function(val) {
2489
- return step(val, function() {
2490
- return _loop(i + 1);
2491
- });
2506
+ return Promise;
2507
+
2508
+ })();
2509
+
2510
+ UnitPromise = (function(_super) {
2511
+ __extends(UnitPromise, _super);
2512
+
2513
+ function UnitPromise() {
2514
+ _ref1 = UnitPromise.__super__.constructor.apply(this, arguments);
2515
+ return _ref1;
2516
+ }
2517
+
2518
+ UnitPromise.prototype.cache = function() {
2519
+ return this;
2520
+ };
2521
+
2522
+ return UnitPromise;
2523
+
2524
+ })(Promise);
2525
+
2526
+ CachingPromise = (function(_super) {
2527
+ __extends(CachingPromise, _super);
2528
+
2529
+ function CachingPromise(uncached) {
2530
+ var _this = this;
2531
+ this.forceRaw = function(fail, cc) {
2532
+ var cacheCC, cacheFail;
2533
+ cacheFail = function(failure) {
2534
+ return (_this.forceRaw = function(f, _) {
2535
+ return new Thunk(function() {
2536
+ return f(failure);
2537
+ });
2538
+ })(fail, cc);
2539
+ };
2540
+ cacheCC = function(val, deps) {
2541
+ return (_this.forceRaw = function(_, s) {
2542
+ return new Thunk(function() {
2543
+ return s(val, deps);
2544
+ });
2545
+ })(fail, cc);
2546
+ };
2547
+ return new Thunk(function() {
2548
+ return uncached(cacheFail, cacheCC);
2492
2549
  });
2493
2550
  };
2494
- return _loop(0);
2551
+ }
2552
+
2553
+ CachingPromise.prototype.cache = function() {
2554
+ return this;
2495
2555
  };
2496
2556
 
2497
- return Promise;
2557
+ return CachingPromise;
2498
2558
 
2499
- })();
2559
+ })(Promise);
2500
2560
 
2501
2561
  Gibbon.Failure = Failure = (function(_super) {
2502
2562
  __extends(Failure, _super);
2503
2563
 
2504
2564
  function Failure() {
2505
- _ref1 = Failure.__super__.constructor.apply(this, arguments);
2506
- return _ref1;
2565
+ _ref2 = Failure.__super__.constructor.apply(this, arguments);
2566
+ return _ref2;
2507
2567
  }
2508
2568
 
2509
2569
  Failure.types({
2510
2570
  query: ['id', 'annotations'],
2511
- composite: ['failures']
2571
+ composite: ['failures'],
2572
+ message: ['message']
2512
2573
  });
2513
2574
 
2514
2575
  Failure.prototype.equals = function(other) {
@@ -2524,6 +2585,9 @@ Gibbon.Failure = Failure = (function(_super) {
2524
2585
  return false;
2525
2586
  }
2526
2587
  },
2588
+ message: function(m) {
2589
+ return m === other.message;
2590
+ },
2527
2591
  composite: function(failures) {
2528
2592
  var failure, i, _i, _len;
2529
2593
  for (i = _i = 0, _len = failures.length; _i < _len; i = ++_i) {
@@ -2545,8 +2609,8 @@ Gibbon.Dependency = Dependency = (function(_super) {
2545
2609
  __extends(Dependency, _super);
2546
2610
 
2547
2611
  function Dependency() {
2548
- _ref2 = Dependency.__super__.constructor.apply(this, arguments);
2549
- return _ref2;
2612
+ _ref3 = Dependency.__super__.constructor.apply(this, arguments);
2613
+ return _ref3;
2550
2614
  }
2551
2615
 
2552
2616
  Dependency.types({
@@ -2597,148 +2661,386 @@ Gibbon.Dependency = Dependency = (function(_super) {
2597
2661
  })(Union);
2598
2662
 
2599
2663
  eval_ = Gibbon["eval"] = (function() {
2600
- var evalAll;
2601
- evalAll = function(semantics, entity, client) {
2602
- var evalFlow, globalPromise, resultThunks, results, toForce;
2603
- results = new Hash;
2604
- globalPromise = Promise.unit(entity);
2605
- resultThunks = new Hash;
2606
- semantics.each(function(key, definition) {
2607
- return resultThunks.set(key, Promise.lazy(function() {
2608
- return evalFlow(definition.flow, globalPromise).then(function(val) {
2609
- return val.resolve();
2664
+ var Context, Scope, combine, unit, vFalse, vTrue;
2665
+ unit = Promise.unit, combine = Promise.combine;
2666
+ vTrue = Value.boolean(true);
2667
+ vFalse = Value.boolean(false);
2668
+ Context = (function() {
2669
+ function Context(client, entity, definitions) {
2670
+ this.client = client;
2671
+ this.entity = entity;
2672
+ this.definitions = definitions;
2673
+ this.rootScope = Scope.global(entity, this);
2674
+ this.definitionBindings = new Hash;
2675
+ }
2676
+
2677
+ Context.prototype.evalAll = function() {
2678
+ var results, toForce,
2679
+ _this = this;
2680
+ this.definitions.each(function(key, ir) {
2681
+ var lazy;
2682
+ lazy = Promise.lazy(function() {
2683
+ return _this.rootScope["eval"](ir).then(function(x) {
2684
+ return x.resolve();
2685
+ });
2610
2686
  });
2611
- }));
2612
- });
2613
- evalFlow = function(flow, global) {
2614
- var input;
2615
- input = flow.tail ? evalFlow(flow.tail, global) : global;
2616
- return flow.head.cases({
2617
- query: function(annotations) {
2618
- return input.then(function(entity) {
2619
- var _this = this;
2620
- return new Promise(function(fail, cc) {
2621
- return client.performQuery(entity.id, annotations, Gibbon.Value, function(err, val) {
2622
- var dependency;
2623
- if (err) {
2624
- return fail(err);
2625
- }
2626
- dependency = Dependency.query(entity, flow.head);
2627
- return val.promise().depends([dependency]).force(fail, cc);
2628
- });
2629
- });
2687
+ return _this.definitionBindings.set(key, lazy.cache());
2688
+ });
2689
+ toForce = [];
2690
+ results = new Hash;
2691
+ this.definitionBindings.each(function(key, promise) {
2692
+ return toForce.push(promise.after(function(val, deps) {
2693
+ deps = uniq(deps, function(x, y) {
2694
+ return x.equals(y);
2630
2695
  });
2696
+ return results.set(key, [val, deps]);
2697
+ }).rescue(function(failure) {
2698
+ results.set(key, [null, [Dependency.failure(failure)]]);
2699
+ return Promise.unit(null);
2700
+ }));
2701
+ });
2702
+ return combine(toForce).then(function() {
2703
+ var deps, val, _ref4;
2704
+ _ref4 = results.get('/'), val = _ref4[0], deps = _ref4[1];
2705
+ if (val) {
2706
+ return Promise.unit(results);
2707
+ } else {
2708
+ return Promise.fail(deps[0].failure);
2709
+ }
2710
+ });
2711
+ };
2712
+
2713
+ Context.prototype.lookup = function(key) {
2714
+ return this.definitionBindings.get(key).dependsOnly([Dependency.lexical(key)]);
2715
+ };
2716
+
2717
+ Context.prototype.performQuery = function(id, annotations) {
2718
+ var _this = this;
2719
+ return new Promise(function(fail, cc) {
2720
+ var isSynchronous, out;
2721
+ isSynchronous = true;
2722
+ out = null;
2723
+ _this.client.performQuery(id, annotations, Value, function(err, data) {
2724
+ var result;
2725
+ if (err) {
2726
+ return (out = fail(err));
2727
+ }
2728
+ result = data.promise().depends([Dependency.query(id, annotations)]);
2729
+ if (isSynchronous) {
2730
+ return out = result.step(fail, cc);
2731
+ } else {
2732
+ return result.force(fail, cc);
2733
+ }
2734
+ });
2735
+ isSynchronous = false;
2736
+ return out;
2737
+ });
2738
+ };
2739
+
2740
+ return Context;
2741
+
2742
+ })();
2743
+ Scope = (function() {
2744
+ var EXTERN, OPERATORS;
2745
+
2746
+ function Scope(bindings, context, parent) {
2747
+ this.bindings = bindings;
2748
+ this.context = context;
2749
+ this.parent = parent;
2750
+ }
2751
+
2752
+ Scope.global = function(entity, context) {
2753
+ return new Scope(new Hash, context, null)["let"]([['@', unit(entity)]]);
2754
+ };
2755
+
2756
+ Scope.prototype.extend = function(b) {
2757
+ if (b == null) {
2758
+ b = new Hash;
2759
+ }
2760
+ return new Scope(b, this.context, this);
2761
+ };
2762
+
2763
+ Scope.prototype["let"] = function(bindings) {
2764
+ var ext, k, v, _i, _len, _ref4;
2765
+ ext = new Hash;
2766
+ for (_i = 0, _len = bindings.length; _i < _len; _i++) {
2767
+ _ref4 = bindings[_i], k = _ref4[0], v = _ref4[1];
2768
+ ext.set(k, v.cache());
2769
+ }
2770
+ return this.extend(ext);
2771
+ };
2772
+
2773
+ OPERATORS = {
2774
+ '+': function(x, y) {
2775
+ return Value.number(x + y);
2776
+ },
2777
+ '*': function(x, y) {
2778
+ return Value.number(x * y);
2779
+ },
2780
+ '/': function(x, y) {
2781
+ return Value.number(x / y);
2782
+ },
2783
+ '<': function(x, y) {
2784
+ return Value.boolean(x < y);
2785
+ },
2786
+ '>': function(x, y) {
2787
+ return Value.boolean(x > y);
2788
+ },
2789
+ '<=': function(x, y) {
2790
+ return Value.boolean(x <= y);
2791
+ },
2792
+ '>=': function(x, y) {
2793
+ return Value.boolean(x >= y);
2794
+ },
2795
+ '===': function(x, y) {
2796
+ return Value.boolean(x === y);
2797
+ }
2798
+ };
2799
+
2800
+ EXTERN = {
2801
+ '-': function(x) {
2802
+ return Value.number(-x);
2803
+ },
2804
+ '!': function(x) {
2805
+ return Value.boolean(!x);
2806
+ }
2807
+ };
2808
+
2809
+ Scope.prototype.get = function(v) {
2810
+ if (this.bindings.has(v)) {
2811
+ return this.bindings.get(v);
2812
+ } else if (this.parent) {
2813
+ return this.parent.get(v);
2814
+ } else {
2815
+ throw "no such variable: " + v;
2816
+ }
2817
+ };
2818
+
2819
+ Scope.prototype.force = function(varName) {
2820
+ return this.get(varName);
2821
+ };
2822
+
2823
+ Scope.prototype["eval"] = function(ir) {
2824
+ var _this = this;
2825
+ return ir.cases({
2826
+ global: function() {
2827
+ return _this.get('@');
2631
2828
  },
2632
- localAccessor: function(key) {
2633
- return new Promise(function(fail, cc) {
2634
- return resultThunks.get(key).force(fail, function(val, otherDeps) {
2635
- return cc(val, [Dependency.lexical(key)]);
2636
- });
2637
- });
2829
+ constant: function(value) {
2830
+ return unit(Value.fromJSON(value));
2638
2831
  },
2639
- func: function(name, args) {
2640
- var a, func;
2641
- func = stdlib[name].impl;
2642
- args = (function() {
2643
- var _i, _len, _results;
2644
- _results = [];
2645
- for (_i = 0, _len = args.length; _i < _len; _i++) {
2646
- a = args[_i];
2647
- _results.push(evalFlow(a, global));
2648
- }
2649
- return _results;
2650
- })();
2651
- return func.apply(null, [input].concat(__slice.call(args)));
2832
+ variable: function(name) {
2833
+ return _this.get(name);
2652
2834
  },
2653
- literal: function(syntax) {
2654
- return syntax.cases({
2655
- integer: function(value) {
2656
- return Promise.unit(Value.number(value));
2657
- },
2658
- decimal: function(value) {
2659
- return Promise.unit(Value.number(value));
2660
- },
2661
- string: function(value) {
2662
- return Promise.unit(Value.string(value));
2835
+ branch: function(cond, ifTrue, ifFalse) {
2836
+ return _this["eval"](cond).then(function(c) {
2837
+ if (c.value) {
2838
+ return _this["eval"](ifTrue);
2839
+ } else {
2840
+ return _this["eval"](ifFalse);
2663
2841
  }
2664
2842
  });
2665
2843
  },
2666
- pair: function(first, second) {
2667
- var eFirst, eSecond;
2668
- eFirst = evalFlow(first, global);
2669
- eSecond = evalFlow(second, global);
2670
- return Promise.unit(Value.pair(eFirst, eSecond));
2844
+ bind: function(name, val, expr) {
2845
+ return _this["eval"](val).then(function(v) {
2846
+ return _this["let"]([[name, v]])["eval"](expr);
2847
+ });
2848
+ },
2849
+ delist: function(expr, index) {
2850
+ var evaled;
2851
+ evaled = [_this["eval"](expr), _this["eval"](index)];
2852
+ return combine(evaled).then(function(_arg) {
2853
+ var elements, value, _ref4, _ref5;
2854
+ (_ref4 = _arg[0], elements = _ref4.elements), (_ref5 = _arg[1], value = _ref5.value);
2855
+ return elements[value];
2856
+ });
2857
+ },
2858
+ depair: function(expr, key) {
2859
+ return _this["eval"](expr).then(function(v) {
2860
+ return v[key];
2861
+ });
2671
2862
  },
2672
- list: function(elements, squished) {
2673
- var e, list;
2674
- list = Promise.unit(Value.list((function() {
2863
+ list: function(elements) {
2864
+ var e;
2865
+ return unit(Value.list((function() {
2675
2866
  var _i, _len, _results;
2676
2867
  _results = [];
2677
2868
  for (_i = 0, _len = elements.length; _i < _len; _i++) {
2678
2869
  e = elements[_i];
2679
- _results.push(evalFlow(e, global));
2870
+ _results.push(this["eval"](e));
2680
2871
  }
2681
2872
  return _results;
2682
- })()));
2683
- if (squished) {
2684
- return stdlib.squish.impl(list);
2685
- } else {
2686
- return list;
2687
- }
2873
+ }).call(_this)));
2874
+ },
2875
+ foldList: function(list, out, arg, accumArg, idxArg, body) {
2876
+ return _this["eval"](list).then(function(_arg) {
2877
+ var elements, _loop;
2878
+ elements = _arg.elements;
2879
+ _loop = function(i) {
2880
+ if (i >= elements.length) {
2881
+ return _this["eval"](out);
2882
+ }
2883
+ return _this["let"]([[idxArg, unit(Value.number(i))], [arg, elements[i]], [accumArg, _loop(i + 1)]])["eval"](body);
2884
+ };
2885
+ return _loop(0);
2886
+ });
2887
+ },
2888
+ mapList: function(list, arg, idxArg, body) {
2889
+ return _this["eval"](list).then(function(_arg) {
2890
+ var el, elements, i, mapped;
2891
+ elements = _arg.elements;
2892
+ mapped = (function() {
2893
+ var _i, _len, _results;
2894
+ _results = [];
2895
+ for (i = _i = 0, _len = elements.length; _i < _len; i = ++_i) {
2896
+ el = elements[i];
2897
+ _results.push(this["let"]([[arg, el], [idxArg, unit(Value.number(i))]])["eval"](body));
2898
+ }
2899
+ return _results;
2900
+ }).call(_this);
2901
+ return unit(Value.list(mapped));
2902
+ });
2903
+ },
2904
+ zipLists: function(firstExpr, secondExpr) {
2905
+ var evaled;
2906
+ evaled = [_this["eval"](firstExpr), _this["eval"](secondExpr)];
2907
+ return combine(evaled).then(function(_arg) {
2908
+ var el, first, i, pairs, second;
2909
+ first = _arg[0], second = _arg[1];
2910
+ pairs = (function() {
2911
+ var _i, _len, _ref4, _results;
2912
+ _ref4 = first.elements;
2913
+ _results = [];
2914
+ for (i = _i = 0, _len = _ref4.length; _i < _len; i = ++_i) {
2915
+ el = _ref4[i];
2916
+ _results.push(Value.pair(el, second.elements[i]));
2917
+ }
2918
+ return _results;
2919
+ })();
2920
+ return unit(Value.list(pairs));
2921
+ });
2922
+ },
2923
+ filterList: function(listExpr, arg, body) {
2924
+ return _this["eval"](listExpr).then(function(_arg) {
2925
+ var el, elements, mapped;
2926
+ elements = _arg.elements;
2927
+ mapped = (function() {
2928
+ var _i, _len, _results,
2929
+ _this = this;
2930
+ _results = [];
2931
+ for (_i = 0, _len = elements.length; _i < _len; _i++) {
2932
+ el = elements[_i];
2933
+ _results.push((function(el) {
2934
+ return _this["let"]([[arg, el]])["eval"](body).map(function(v) {
2935
+ if (v.value) {
2936
+ return [el];
2937
+ } else {
2938
+ return [];
2939
+ }
2940
+ });
2941
+ })(el));
2942
+ }
2943
+ return _results;
2944
+ }).call(_this);
2945
+ return combine(mapped).map(function(mappedEls) {
2946
+ return Value.list(catLists(mappedEls));
2947
+ });
2948
+ });
2949
+ },
2950
+ squishList: function(listExpr) {
2951
+ return _this["eval"](listExpr).then(function(_arg) {
2952
+ var elements, promise, wrapped;
2953
+ elements = _arg.elements;
2954
+ wrapped = (function() {
2955
+ var _i, _len, _results,
2956
+ _this = this;
2957
+ _results = [];
2958
+ for (_i = 0, _len = elements.length; _i < _len; _i++) {
2959
+ promise = elements[_i];
2960
+ _results.push(promise.then(function(e) {
2961
+ return e.resolve().map(function(e) {
2962
+ return [e.promise()];
2963
+ });
2964
+ }).or(unit([])));
2965
+ }
2966
+ return _results;
2967
+ }).call(_this);
2968
+ return combine(wrapped).map(function(lists) {
2969
+ return Value.list(catLists(lists));
2970
+ });
2971
+ });
2972
+ },
2973
+ len: function(list) {
2974
+ return _this["eval"](list).map(function(_arg) {
2975
+ var elements;
2976
+ elements = _arg.elements;
2977
+ return Value.number(elements.length);
2978
+ });
2979
+ },
2980
+ pair: function(first, second) {
2981
+ return unit(Value.pair(_this["eval"](first), _this["eval"](second)));
2688
2982
  },
2689
- block: function(body) {
2690
- return Promise.unit(Value.block(function(inputPromise) {
2691
- return evalFlow(body, inputPromise);
2983
+ block: function(name, body) {
2984
+ return unit(Value.block(function(input) {
2985
+ return _this["let"]([['@', input], [name, input]])["eval"](body);
2692
2986
  }));
2693
2987
  },
2694
- defaulted: function(body, alt) {
2695
- return evalFlow(body, global).or(evalFlow(alt, global));
2988
+ app: function(block, arg) {
2989
+ arg = _this["eval"](arg);
2990
+ return _this["eval"](block).then(function(b) {
2991
+ return b.fn(arg);
2992
+ });
2993
+ },
2994
+ query: function(expr, annotations) {
2995
+ return _this["eval"](expr).then(function(entity) {
2996
+ return _this.context.performQuery(entity.id, annotations);
2997
+ });
2998
+ },
2999
+ localQuery: function(key) {
3000
+ return _this.context.lookup(key);
3001
+ },
3002
+ fail: function(message) {
3003
+ return Promise.fail(Failure.message(message));
3004
+ },
3005
+ binop: function(op, lhs, rhs) {
3006
+ return combine([_this["eval"](lhs), _this["eval"](rhs)]).map(function(_arg) {
3007
+ var l, r;
3008
+ l = _arg[0], r = _arg[1];
3009
+ return OPERATORS[op](l.value, r.value);
3010
+ });
3011
+ },
3012
+ extern: function(name, value) {
3013
+ return _this["eval"](value).map(function(v) {
3014
+ return EXTERN[name](v.value);
3015
+ });
3016
+ },
3017
+ rescue: function(expr, default_) {
3018
+ return _this["eval"](expr).or(_this["eval"](default_));
2696
3019
  }
2697
3020
  });
2698
3021
  };
2699
- toForce = [];
2700
- resultThunks.each(function(key, thunk) {
2701
- return toForce.push(thunk.rescue(function(failure) {
2702
- results.set(key, [null, [Dependency.failure(failure)]]);
2703
- resultThunks.set(key, Promise.fail(failure));
2704
- return Promise.unit(null);
2705
- }).after(function(val, deps) {
2706
- deps = uniq(deps, function(x, y) {
2707
- return x.equals(y);
2708
- });
2709
- return results.set(key, [val, deps]);
2710
- }));
2711
- });
2712
- return Promise.combine(toForce).map(function() {
2713
- return results;
2714
- });
2715
- };
3022
+
3023
+ return Scope;
3024
+
3025
+ })();
2716
3026
  return function(semantics, table, id, client, finish) {
2717
- var entity, key, onFailure, onSuccess, _i, _len, _ref3;
2718
- _ref3 = semantics.keys();
2719
- for (_i = 0, _len = _ref3.length; _i < _len; _i++) {
2720
- key = _ref3[_i];
2721
- semantics.modify(key, function(val) {
2722
- if (val instanceof Semantic) {
2723
- return val;
2724
- }
2725
- return Semantic.fromJSON(val);
2726
- });
2727
- }
3027
+ var allCompiled, compile, entity, onFailure, onSuccess, optimize;
3028
+ optimize = Gibbon.optimize, compile = Gibbon.compile;
3029
+ allCompiled = new Hash;
3030
+ semantics.each(function(key, semantic) {
3031
+ var compiled;
3032
+ compiled = optimize(compile(semantic));
3033
+ return allCompiled.set(key, compiled);
3034
+ });
2728
3035
  entity = Value.entity(table, id);
2729
3036
  onFailure = function(fail) {
2730
3037
  return finish(fail);
2731
3038
  };
2732
3039
  onSuccess = function(vals) {
2733
- var deps, val, _ref4;
2734
- _ref4 = vals.get('/'), val = _ref4[0], deps = _ref4[1];
2735
- if (val) {
2736
- return finish(null, vals);
2737
- } else {
2738
- return finish(deps[0].failure);
2739
- }
3040
+
3041
+ return finish(null, vals);
2740
3042
  };
2741
- return evalAll(semantics, entity, client).force(onFailure, onSuccess);
3043
+ return new Context(client, entity, allCompiled).evalAll().force(onFailure, onSuccess);
2742
3044
  };
2743
3045
  })();
2744
3046
  // Generated by CoffeeScript 1.6.3
@@ -2775,7 +3077,7 @@ Gibbon.IR = IR = (function(_super) {
2775
3077
  block: ['name', 'body'],
2776
3078
  app: ['block', 'arg'],
2777
3079
  query: ['expr', 'annotations'],
2778
- localQuery: ['expr', 'key'],
3080
+ localQuery: ['key'],
2779
3081
  fail: ['message'],
2780
3082
  binop: ['op', 'lhs', 'rhs'],
2781
3083
  extern: ['name', 'value'],
@@ -2802,10 +3104,6 @@ Gibbon.IR = IR = (function(_super) {
2802
3104
  return IR.query(this, annotations);
2803
3105
  };
2804
3106
 
2805
- IR.prototype.localQuery = function(key) {
2806
- return IR.localQuery(this, key);
2807
- };
2808
-
2809
3107
  IR.prototype.binop = function(op, other) {
2810
3108
  return IR.binop(op, this, other);
2811
3109
  };
@@ -2826,11 +3124,56 @@ Gibbon.IR = IR = (function(_super) {
2826
3124
  return IR.len(this);
2827
3125
  };
2828
3126
 
2829
- IR.prototype.seq = function(name, f) {
2830
- name = nameGen(name);
2831
- return IR.bind(name, this, f(IR.variable(name)));
2832
- };
2833
-
3127
+ IR.prototype.isAsync = function() {
3128
+ return this.cases({
3129
+ query: function() {
3130
+ return true;
3131
+ },
3132
+ localQuery: function() {
3133
+ return true;
3134
+ },
3135
+ other: function() {
3136
+ var sub, _i, _len, _ref1;
3137
+ _ref1 = this.subtrees();
3138
+ for (_i = 0, _len = _ref1.length; _i < _len; _i++) {
3139
+ sub = _ref1[_i];
3140
+ if (sub.isAsync()) {
3141
+ return true;
3142
+ }
3143
+ }
3144
+ return false;
3145
+ }
3146
+ });
3147
+ };
3148
+
3149
+ IR.prototype.isSimple = function() {
3150
+ return this.cases({
3151
+ variable: function() {
3152
+ return true;
3153
+ },
3154
+ constant: function() {
3155
+ return true;
3156
+ },
3157
+ global: function() {
3158
+ return true;
3159
+ },
3160
+ binop: function(op, l, r) {
3161
+ return l.isSimple() && r.isSimple();
3162
+ },
3163
+ extern: function(n, v) {
3164
+ return v.isSimple();
3165
+ },
3166
+ other: function() {
3167
+ return false;
3168
+ }
3169
+ });
3170
+ };
3171
+
3172
+ IR.prototype.seq = function(name, f) {
3173
+ name = nameGen(name);
3174
+ return IR.bind(name, this, f(IR.variable(name)));
3175
+ };
3176
+
2834
3177
  IR.prototype.mapList = function(f) {
2835
3178
  var elName, ixName;
2836
3179
  elName = nameGen('el');
@@ -2891,7 +3234,6 @@ Gibbon.IR = IR = (function(_super) {
2891
3234
  },
2892
3235
  app: double,
2893
3236
  query: single,
2894
- localQuery: single,
2895
3237
  binop: function(op, l, r) {
2896
3238
  return [l, r];
2897
3239
  },
@@ -2955,9 +3297,6 @@ Gibbon.IR = IR = (function(_super) {
2955
3297
  query: function(e, a) {
2956
3298
  return IR.query(f(e), a);
2957
3299
  },
2958
- localQuery: function(e, k) {
2959
- return IR.localQuery(f(e), k);
2960
- },
2961
3300
  binop: function(o, l, r) {
2962
3301
  return IR.binop(o, f(l), f(r));
2963
3302
  },
@@ -2993,17 +3332,25 @@ Gibbon.IR = IR = (function(_super) {
2993
3332
  return false;
2994
3333
  }
2995
3334
  return this.cases({
3335
+ global: function() {
3336
+ return true;
3337
+ },
3338
+ constant: function(v) {
3339
+ return v === other.value;
3340
+ },
3341
+ variable: function(name) {
3342
+ return name === other.name;
3343
+ },
2996
3344
  branch: function(cond, ifTrue, ifFalse) {
2997
3345
  return cond.equals(other.cond) && ifTrue.equals(other.ifTrue) && ifFalse.equals(other.ifFalse);
2998
3346
  },
2999
3347
  bind: function(name, val, expr) {
3348
+ var subst;
3000
3349
  if (!val.equals(other.value)) {
3001
3350
  return false;
3002
3351
  }
3003
- return expr.equals(other.replace(IR.variable(other.name), IR.variable(name)));
3004
- },
3005
- variable: function(name) {
3006
- return name === other.name;
3352
+ subst = other.expr.subst(other.name, IR.variable(name));
3353
+ return expr.equals(subst);
3007
3354
  },
3008
3355
  delist: function(expr, index) {
3009
3356
  return expr.equals(other.expr) && index.equals(other.index);
@@ -3021,14 +3368,66 @@ Gibbon.IR = IR = (function(_super) {
3021
3368
  }
3022
3369
  return true;
3023
3370
  },
3371
+ foldList: function(list, out, arg, accumArg, idxArg, body) {
3372
+ var subst;
3373
+ if (!list.equals(other.list)) {
3374
+ return false;
3375
+ }
3376
+ if (!out.equals(other.out)) {
3377
+ return false;
3378
+ }
3379
+ subst = other.body.subst(other.arg, IR.variable(arg)).subst(other.accumArg, IR.variable(accumArg)).subst(other.idxArg, IR.variable(idxArg));
3380
+ return body.equals(subst);
3381
+ },
3382
+ mapList: function(list, arg, idxArg, body) {
3383
+ var subst;
3384
+ if (!list.equals(other.list)) {
3385
+ return false;
3386
+ }
3387
+ subst = other.body.subst(other.arg, IR.variable(arg)).subst(other.idxArg, IR.variable(idxArg));
3388
+ return body.equals(subst);
3389
+ },
3390
+ zipLists: function(first, second) {
3391
+ return first.equals(other.first) && second.equals(other.second);
3392
+ },
3393
+ filterList: function(list, arg, body) {
3394
+ if (!list.equals(other.list)) {
3395
+ return false;
3396
+ }
3397
+ return body.equals(other.body.subst(other.arg, IR.variable(arg)));
3398
+ },
3399
+ squishList: function(list) {
3400
+ return list.equals(other.list);
3401
+ },
3402
+ len: function(list) {
3403
+ return list.equals(other.list);
3404
+ },
3024
3405
  pair: function(x, y) {
3025
3406
  return x.equals(other.first) && y.equals(other.second);
3026
3407
  },
3408
+ block: function(name, body) {
3409
+ return false;
3410
+ },
3411
+ app: function(block, arg) {
3412
+ return block.equals(other.block) && arg.equals(other.arg);
3413
+ },
3027
3414
  query: function(expr, annotations) {
3028
3415
  return JSON.stringify(annotations) === JSON.stringify(other.annotations) && expr.equals(other.expr);
3029
3416
  },
3030
- localQuery: function(expr, key) {
3031
- return key === other.key && expr.equals(other.expr);
3417
+ localQuery: function(key) {
3418
+ return key === other.key;
3419
+ },
3420
+ fail: function() {
3421
+ return false;
3422
+ },
3423
+ binop: function(op, lhs, rhs) {
3424
+ return op === other.op && lhs.equals(other.lhs) && rhs.equals(other.rhs);
3425
+ },
3426
+ extern: function(name, value) {
3427
+ return name === other.name && value.equals(other.value);
3428
+ },
3429
+ rescue: function(expr, default_) {
3430
+ return expr.equals(other.expr) && default_.equals(other["default"]);
3032
3431
  }
3033
3432
  });
3034
3433
  };
@@ -3059,7 +3458,7 @@ Gibbon.IR = IR = (function(_super) {
3059
3458
  return name;
3060
3459
  },
3061
3460
  delist: function(e, i) {
3062
- return "([" + i + "] " + (e.inspect()) + ")";
3461
+ return "([" + (i.inspect()) + "] " + (e.inspect()) + ")";
3063
3462
  },
3064
3463
  depair: function(e, k) {
3065
3464
  return "([" + k + "] " + (e.inspect()) + ")";
@@ -3085,9 +3484,15 @@ Gibbon.IR = IR = (function(_super) {
3085
3484
  foldList: function(list, out, arg, next, i, body) {
3086
3485
  return "(FOLDR " + (list.inspect()) + " " + (out.inspect()) + " " + arg + " " + next + " " + i + " " + (body.inspect()) + ")";
3087
3486
  },
3487
+ filterList: function(list, arg, body) {
3488
+ return "(FILTER " + (list.inspect()) + " " + arg + " " + (body.inspect()) + ")";
3489
+ },
3088
3490
  zipLists: function(x, y) {
3089
3491
  return "(ZIP " + (x.inspect()) + " " + (y.inspect()) + ")";
3090
3492
  },
3493
+ squishList: function(l) {
3494
+ return "(SQUISH " + (l.inspect()) + ")";
3495
+ },
3091
3496
  pair: function(x, y) {
3092
3497
  return "(PAIR " + (x.inspect()) + " " + (y.inspect()) + ")";
3093
3498
  },
@@ -3101,7 +3506,7 @@ Gibbon.IR = IR = (function(_super) {
3101
3506
  return "(APPLY " + (b.inspect()) + " " + (a.inspect()) + ")";
3102
3507
  },
3103
3508
  localQuery: function(k) {
3104
- return "(@" + k + " " + (e.inspect()) + ")";
3509
+ return "@" + k;
3105
3510
  },
3106
3511
  fail: function(m) {
3107
3512
  return "(FAIL " + m + ")";
@@ -3149,10 +3554,10 @@ Gibbon.compile = (function() {
3149
3554
  return input.query(annotations);
3150
3555
  },
3151
3556
  localAccessor: function(key) {
3152
- return input.localQuery(key);
3557
+ return IR.localQuery(key);
3153
3558
  },
3154
- func: function(name, args) {
3155
- var arg, argTypes, compArgs;
3559
+ func: function(name, args, tvars) {
3560
+ var arg, compArgs;
3156
3561
  compArgs = (function() {
3157
3562
  var _i, _len, _results;
3158
3563
  _results = [];
@@ -3162,23 +3567,14 @@ Gibbon.compile = (function() {
3162
3567
  }
3163
3568
  return _results;
3164
3569
  })();
3165
- argTypes = (function() {
3166
- var _i, _len, _results;
3167
- _results = [];
3168
- for (_i = 0, _len = args.length; _i < _len; _i++) {
3169
- arg = args[_i];
3170
- _results.push(arg.type);
3171
- }
3172
- return _results;
3173
- })();
3174
- return stdlib[name].compile(input, compArgs, this.type, argTypes);
3570
+ return stdlib[name].compile(input, compArgs, tvars);
3175
3571
  },
3176
3572
  literal: function(syntax) {
3177
3573
  return IR.constant(syntax.value);
3178
3574
  },
3179
- list: function(elements) {
3180
- var e;
3181
- return IR.list((function() {
3575
+ list: function(elements, squish) {
3576
+ var e, list;
3577
+ list = IR.list((function() {
3182
3578
  var _i, _len, _results;
3183
3579
  _results = [];
3184
3580
  for (_i = 0, _len = elements.length; _i < _len; _i++) {
@@ -3187,6 +3583,11 @@ Gibbon.compile = (function() {
3187
3583
  }
3188
3584
  return _results;
3189
3585
  })());
3586
+ if (squish) {
3587
+ return list.squish();
3588
+ } else {
3589
+ return list;
3590
+ }
3190
3591
  },
3191
3592
  pair: function(x, y) {
3192
3593
  return IR.pair(compile(x, context), compile(y, context));
@@ -3201,12 +3602,315 @@ Gibbon.compile = (function() {
3201
3602
  }
3202
3603
  });
3203
3604
  };
3204
- return function(semantics) {
3205
- var out;
3206
- out = new Hash;
3207
- semantics.each(function(k, v) {
3208
- return out.set(k, compile(v));
3605
+ return function(semantic) {
3606
+ return compile(semantic);
3607
+ };
3608
+ })();
3609
+ // Generated by CoffeeScript 1.6.3
3610
+ Gibbon.optimize = (function() {
3611
+ var insertBindings, partialEval;
3612
+ insertBindings = function(expr) {
3613
+ return expr;
3614
+ };
3615
+ partialEval = function(expr) {
3616
+ return expr.cases({
3617
+ depair: function(expr, key) {
3618
+ return partialEval(expr).cases({
3619
+ pair: function() {
3620
+
3621
+ return this[key];
3622
+ },
3623
+ other: function() {
3624
+ return this.depair(key);
3625
+ }
3626
+ });
3627
+ },
3628
+ delist: function(expr, index) {
3629
+ return partialEval(expr).cases({
3630
+ list: function(els) {
3631
+
3632
+ return els[index];
3633
+ },
3634
+ other: function() {
3635
+ return this.delist(index);
3636
+ }
3637
+ });
3638
+ },
3639
+ branch: function(cond, ifTrue, ifFalse) {
3640
+ ifTrue = partialEval(ifTrue);
3641
+ ifFalse = partialEval(ifFalse);
3642
+ if (ifTrue.equals(ifFalse)) {
3643
+
3644
+ return ifTrue;
3645
+ }
3646
+ return partialEval(cond).cases({
3647
+ constant: function(value) {
3648
+ if (value) {
3649
+
3650
+ return ifTrue;
3651
+ } else {
3652
+
3653
+ return ifFalse;
3654
+ }
3655
+ },
3656
+ branch: function(innerCond, innerTrue, innerFalse) {
3657
+ if (innerTrue._tag === 'constant' && innerFalse._tag === 'constant') {
3658
+
3659
+ if (innerTrue.value) {
3660
+ return partialEval(innerCond.branch(ifTrue, ifFalse));
3661
+ } else {
3662
+ return partialEval(innerCond.branch(ifFalse, ifTrue));
3663
+ }
3664
+ } else {
3665
+ return this.branch(ifTrue, ifFalse);
3666
+ }
3667
+ },
3668
+ extern: function(name, arg) {
3669
+ if (name === '!') {
3670
+
3671
+ return partialEval(arg.branch(ifFalse, ifTrue));
3672
+ } else {
3673
+ return this.branch(ifTrue, ifFalse);
3674
+ }
3675
+ },
3676
+ other: function() {
3677
+ return this.branch(ifTrue, ifFalse);
3678
+ }
3679
+ });
3680
+ },
3681
+ app: function(block, arg) {
3682
+ return partialEval(block).cases({
3683
+ block: function(argName, body) {
3684
+ return partialEval(body.subst(argName, arg));
3685
+ },
3686
+ other: function() {
3687
+ return this.app(partialEval(arg));
3688
+ }
3689
+ });
3690
+ },
3691
+ mapList: function(list, arg, ixName, body) {
3692
+ return list.cases({
3693
+ list: function(elements) {
3694
+ var el, i, mapped, _i, _len;
3695
+
3696
+ for (i = _i = 0, _len = elements.length; _i < _len; i = ++_i) {
3697
+ el = elements[i];
3698
+
3699
+ partialEval(body.subst(arg, el).subst(ixName, IR.constant(i)));
3700
+ }
3701
+ mapped = (function() {
3702
+ var _j, _len1, _results;
3703
+ _results = [];
3704
+ for (_j = 0, _len1 = elements.length; _j < _len1; _j++) {
3705
+ el = elements[_j];
3706
+ _results.push(body.subst(arg, el));
3707
+ }
3708
+ return _results;
3709
+ })();
3710
+ return partialEval(IR.list(mapped));
3711
+ },
3712
+ other: function() {
3713
+ return IR.mapList(this, arg, ixName, partialEval(body));
3714
+ }
3715
+ });
3716
+ },
3717
+ foldList: function(list, out, arg, accum, ixName, body) {
3718
+ return partialEval(list).cases({
3719
+ list: function(elements) {
3720
+ var _loop;
3721
+
3722
+ _loop = function(i) {
3723
+ var next;
3724
+ if (i >= elements.length) {
3725
+ return partialEval(out);
3726
+ }
3727
+ next = _loop(i + 1);
3728
+
3729
+ return body.subst(arg, elements[i]).subst(accum, next).subst(ixName, IR.constant(i));
3730
+ };
3731
+ return partialEval(_loop(0));
3732
+ },
3733
+ other: function() {
3734
+ return IR.foldList(this, partialEval(out), arg, accum, ixName, partialEval(body));
3735
+ }
3736
+ });
3737
+ },
3738
+ len: function(list) {
3739
+ return partialEval(list).cases({
3740
+ list: function(elements) {
3741
+
3742
+ return IR.constant(elements.length);
3743
+ },
3744
+ mapList: function(list) {
3745
+
3746
+ return partialEval(list.len());
3747
+ },
3748
+ zipLists: function(l, r) {
3749
+
3750
+ return partialEval(l.len());
3751
+ },
3752
+ other: function() {
3753
+ return this.len();
3754
+ }
3755
+ });
3756
+ },
3757
+ zipLists: function(l, r) {
3758
+ var e, elements, i;
3759
+ l = partialEval(l);
3760
+ r = partialEval(r);
3761
+ if (!(l._tag === 'list' && r._tag === 'list')) {
3762
+ return IR.zipLists(l, r);
3763
+ }
3764
+ elements = (function() {
3765
+ var _i, _len, _results;
3766
+ _results = [];
3767
+ for (i = _i = 0, _len = l.length; _i < _len; i = ++_i) {
3768
+ e = l[i];
3769
+ _results.push(IR.pair(e, r[i]));
3770
+ }
3771
+ return _results;
3772
+ })();
3773
+ return partialEval(IR.list(elements));
3774
+ },
3775
+ extern: function(op, arg) {
3776
+ return partialEval(arg).cases({
3777
+ constant: function(value) {
3778
+ if (/Math\.(\w+)/.test(op)) {
3779
+ return IR.constant(Math[RegExp.$1](value));
3780
+ }
3781
+ return IR.constant((function() {
3782
+ switch (op) {
3783
+ case '!':
3784
+ return !value;
3785
+ case '-':
3786
+ return -value;
3787
+ default:
3788
+ throw "unknown operator " + op;
3789
+ }
3790
+ })());
3791
+ },
3792
+ other: function() {
3793
+ return IR.extern(op, this);
3794
+ }
3795
+ });
3796
+ },
3797
+ binop: function(op, left, right) {
3798
+ var l, r, _ref;
3799
+ left = partialEval(left);
3800
+ right = partialEval(right);
3801
+ if (!(left._tag === 'constant' && right._tag === 'constant')) {
3802
+ return IR.binop(op, left, right);
3803
+ }
3804
+
3805
+ _ref = [left.value, right.value], l = _ref[0], r = _ref[1];
3806
+ return IR.constant((function() {
3807
+ switch (op) {
3808
+ case '+':
3809
+ return l + r;
3810
+ case '*':
3811
+ return l * r;
3812
+ case '/':
3813
+ return l / r;
3814
+ case '%':
3815
+ return l % r;
3816
+ case '===':
3817
+ return l === r;
3818
+ case '<':
3819
+ return l < r;
3820
+ case '>':
3821
+ return l > r;
3822
+ case '<=':
3823
+ return l <= r;
3824
+ case '>=':
3825
+ return l >= r;
3826
+ }
3827
+ })());
3828
+ },
3829
+ rescue: function(expr, default_) {
3830
+ var alwaysFails, alwaysSucceeds;
3831
+ expr = partialEval(expr);
3832
+ alwaysSucceeds = function(e) {
3833
+ return e.cases({
3834
+ query: function() {
3835
+ return false;
3836
+ },
3837
+ localQuery: function() {
3838
+ return false;
3839
+ },
3840
+ fail: function() {
3841
+ return false;
3842
+ },
3843
+ rescue: function() {
3844
+ return true;
3845
+ },
3846
+ squishList: function() {
3847
+ return true;
3848
+ },
3849
+ other: function() {
3850
+ var subtree, _i, _len, _ref;
3851
+ _ref = this.subtrees();
3852
+ for (_i = 0, _len = _ref.length; _i < _len; _i++) {
3853
+ subtree = _ref[_i];
3854
+ console.log("checking alwaysSucceeds " + (subtree.inspect()));
3855
+ if (!alwaysSucceeds(subtree)) {
3856
+ return false;
3857
+ }
3858
+ }
3859
+ return true;
3860
+ }
3861
+ });
3862
+ };
3863
+ alwaysFails = function(e) {
3864
+ return e.cases({
3865
+ query: function() {
3866
+ return false;
3867
+ },
3868
+ localQuery: function() {
3869
+ return false;
3870
+ },
3871
+ fail: function() {
3872
+ return true;
3873
+ },
3874
+ rescue: function() {
3875
+ return false;
3876
+ },
3877
+ squishList: function() {
3878
+ return false;
3879
+ },
3880
+ other: function() {
3881
+ var subtree, _i, _len, _ref;
3882
+ _ref = this.subtrees();
3883
+ for (_i = 0, _len = _ref.length; _i < _len; _i++) {
3884
+ subtree = _ref[_i];
3885
+ console.log("checking alwaysFails " + (subtree.inspect()));
3886
+ if (alwaysFails(subtree)) {
3887
+ return true;
3888
+ }
3889
+ }
3890
+ return false;
3891
+ }
3892
+ });
3893
+ };
3894
+ if (alwaysSucceeds(expr)) {
3895
+
3896
+ return expr;
3897
+ } else if (alwaysFails(expr)) {
3898
+
3899
+ return partialEval(default_);
3900
+ } else {
3901
+ return IR.rescue(expr, partialEval(default_));
3902
+ }
3903
+ },
3904
+ other: function() {
3905
+ return this.map(partialEval);
3906
+ }
3209
3907
  });
3908
+ };
3909
+ return function(expr) {
3910
+ var out;
3911
+
3912
+ out = insertBindings(partialEval(expr));
3913
+
3210
3914
  return out;
3211
3915
  };
3212
3916
  })();
@@ -3214,42 +3918,11 @@ Gibbon.compile = (function() {
3214
3918
  var stdlib;
3215
3919
 
3216
3920
  stdlib = Gibbon.stdlib = (function() {
3217
- var allBooleans, anyBooleans, combine, compEquals, iFalse, iTrue, iter, unit, vFalse, vTrue;
3218
- unit = Promise.unit;
3219
- iter = Promise.iter;
3220
- combine = Promise.combine;
3921
+ var compEquals, iFalse, iTrue, vFalse, vTrue;
3221
3922
  vTrue = Value.boolean(true);
3222
3923
  vFalse = Value.boolean(false);
3223
3924
  iTrue = IR.constant(true);
3224
3925
  iFalse = IR.constant(false);
3225
- anyBooleans = function(promises) {
3226
- var out, step;
3227
- step = function(cond, next) {
3228
- if (cond.value) {
3229
- return unit(vTrue);
3230
- } else {
3231
- return next();
3232
- }
3233
- };
3234
- out = function() {
3235
- return unit(vFalse);
3236
- };
3237
- return iter(promises, step, out);
3238
- };
3239
- allBooleans = function(promises) {
3240
- var out, step;
3241
- step = function(cond, next) {
3242
- if (cond.value) {
3243
- return next();
3244
- } else {
3245
- return unit(vFalse);
3246
- }
3247
- };
3248
- out = function() {
3249
- return unit(vTrue);
3250
- };
3251
- return iter(promises, step, out);
3252
- };
3253
3926
  compEquals = function(x, y, type) {
3254
3927
  var direct;
3255
3928
  direct = function() {
@@ -3282,24 +3955,6 @@ stdlib = Gibbon.stdlib = (function() {
3282
3955
  return {
3283
3956
  "case": {
3284
3957
  type: parse.type('case [bool : %b] = % -> %b'),
3285
- impl: function(_, evalList) {
3286
- return evalList.then(function(list) {
3287
- var out, step;
3288
- step = function(pair, next) {
3289
- return pair.first.then(function(cond) {
3290
- if (cond.value) {
3291
- return pair.second;
3292
- } else {
3293
- return next();
3294
- }
3295
- });
3296
- };
3297
- out = function() {
3298
- return Promise.fail('non-exhaustive cases');
3299
- };
3300
- return iter(list.elements, step, out);
3301
- });
3302
- },
3303
3958
  compile: function(_, _arg) {
3304
3959
  var alts;
3305
3960
  alts = _arg[0];
@@ -3310,32 +3965,10 @@ stdlib = Gibbon.stdlib = (function() {
3310
3965
  },
3311
3966
  "case-eq": {
3312
3967
  type: parse.type('case-eq [%a : %b] = %a -> %b'),
3313
- impl: function(evalInput, evalList) {
3314
- return combine([evalInput, evalList]).then(function(_arg) {
3315
- var input, list, out, step;
3316
- input = _arg[0], list = _arg[1];
3317
- step = function(pair, next) {
3318
- return pair.first.then(function(test) {
3319
- return input.equals(test).then(function(vBool) {
3320
- if (vBool.value) {
3321
- return pair.second;
3322
- } else {
3323
- return next();
3324
- }
3325
- });
3326
- });
3327
- };
3328
- out = function() {
3329
- return Promise.fail('non-exhaustive cases');
3330
- };
3331
- return iter(list.elements, step, out);
3332
- });
3333
- },
3334
- compile: function(input, _arg, _, _arg1) {
3335
- var alts, argType, eqType;
3968
+ compile: function(input, _arg, tvars) {
3969
+ var alts, eqType;
3336
3970
  alts = _arg[0];
3337
- argType = _arg1[0];
3338
- eqType = argType.of.first;
3971
+ eqType = tvars.get('a');
3339
3972
  return alts.foldList(IR.fail('non-exhaustive cases'), function(el, next) {
3340
3973
  var first, second;
3341
3974
  first = el.depair('first');
@@ -3346,14 +3979,10 @@ stdlib = Gibbon.stdlib = (function() {
3346
3979
  },
3347
3980
  "case-eq-default": {
3348
3981
  type: parse.type('case-eq-default %b [%a : %b] = %a -> %b'),
3349
- impl: function(evalInput, evalDefault, evalList) {
3350
- return stdlib['case-eq'].impl(evalInput, evalList).or(evalDefault);
3351
- },
3352
- compile: function(input, _arg, _, _arg1) {
3353
- var alts, argType, default_, eqType;
3982
+ compile: function(input, _arg, tvars) {
3983
+ var alts, default_, eqType;
3354
3984
  default_ = _arg[0], alts = _arg[1];
3355
- argType = _arg1[0];
3356
- eqType = argType.of.first;
3985
+ eqType = tvars.get('a');
3357
3986
  return alts.foldList(default_, function(el, next) {
3358
3987
  var first, second;
3359
3988
  first = el.depair('first');
@@ -3364,11 +3993,6 @@ stdlib = Gibbon.stdlib = (function() {
3364
3993
  },
3365
3994
  "any-true?": {
3366
3995
  type: parse.type('any-true? = [bool] -> bool'),
3367
- impl: function(evalList) {
3368
- return evalList.then(function(list) {
3369
- return anyBooleans(list.elements);
3370
- });
3371
- },
3372
3996
  compile: function(list) {
3373
3997
  return list.foldList(iFalse, function(el, next) {
3374
3998
  return el.branch(iTrue, next);
@@ -3377,11 +4001,6 @@ stdlib = Gibbon.stdlib = (function() {
3377
4001
  },
3378
4002
  "all-true?": {
3379
4003
  type: parse.type('all-true? = [bool] -> bool'),
3380
- impl: function(evalList) {
3381
- return evalList.then(function(list) {
3382
- return allBooleans(list.elements);
3383
- });
3384
- },
3385
4004
  compile: function(list) {
3386
4005
  return list.foldList(iTrue, function(el, next) {
3387
4006
  return el.branch(next, iFalse);
@@ -3390,24 +4009,9 @@ stdlib = Gibbon.stdlib = (function() {
3390
4009
  },
3391
4010
  "any?": {
3392
4011
  type: parse.type('any? { %a -> bool } = [%a] -> bool'),
3393
- impl: function(evalList, evalBlock) {
3394
- return combine([evalList, evalBlock]).then(function(_arg) {
3395
- var block, e, fn, list;
3396
- list = _arg[0], block = _arg[1];
3397
- fn = block.fn;
3398
- return anyBooleans((function() {
3399
- var _i, _len, _ref, _results;
3400
- _ref = list.elements;
3401
- _results = [];
3402
- for (_i = 0, _len = _ref.length; _i < _len; _i++) {
3403
- e = _ref[_i];
3404
- _results.push(fn(e));
3405
- }
3406
- return _results;
3407
- })());
3408
- });
3409
- },
3410
- compile: function(list, block) {
4012
+ compile: function(list, _arg) {
4013
+ var block;
4014
+ block = _arg[0];
3411
4015
  return list.foldList(iFalse, function(el, next) {
3412
4016
  return block.app(el).branch(iTrue, next);
3413
4017
  });
@@ -3415,24 +4019,9 @@ stdlib = Gibbon.stdlib = (function() {
3415
4019
  },
3416
4020
  "all?": {
3417
4021
  type: parse.type('all? { %a -> bool } = [%a] -> bool'),
3418
- impl: function(evalList, evalBlock) {
3419
- return combine([evalList, evalBlock]).then(function(_arg) {
3420
- var block, e, fn, list;
3421
- list = _arg[0], block = _arg[1];
3422
- fn = block.fn;
3423
- return allBooleans((function() {
3424
- var _i, _len, _ref, _results;
3425
- _ref = list.elements;
3426
- _results = [];
3427
- for (_i = 0, _len = _ref.length; _i < _len; _i++) {
3428
- e = _ref[_i];
3429
- _results.push(fn(e));
3430
- }
3431
- return _results;
3432
- })());
3433
- });
3434
- },
3435
- compile: function(list, block) {
4022
+ compile: function(list, _arg) {
4023
+ var block;
4024
+ block = _arg[0];
3436
4025
  return list.foldList(iTrue, function(el, next) {
3437
4026
  return block.app(el).branch(next, iFalse);
3438
4027
  });
@@ -3440,40 +4029,17 @@ stdlib = Gibbon.stdlib = (function() {
3440
4029
  },
3441
4030
  "include?": {
3442
4031
  type: parse.type('include? %a = [%a] -> bool'),
3443
- impl: function(evalList, evalElement) {
3444
- return combine([evalElement, evalList]).then(function(_arg) {
3445
- var booleans, el, list, p;
3446
- el = _arg[0], list = _arg[1];
3447
- booleans = (function() {
3448
- var _i, _len, _ref, _results;
3449
- _ref = list.elements;
3450
- _results = [];
3451
- for (_i = 0, _len = _ref.length; _i < _len; _i++) {
3452
- p = _ref[_i];
3453
- _results.push(p.then(function(x) {
3454
- return el.equals(x);
3455
- }));
3456
- }
3457
- return _results;
3458
- })();
3459
- return anyBooleans(booleans);
3460
- });
3461
- },
3462
- compile: function(list, needle, _, _arg) {
3463
- var listType;
3464
- listType = _arg[0];
4032
+ compile: function(list, _arg, tvars) {
4033
+ var elType, needle;
4034
+ needle = _arg[0];
4035
+ elType = tvars.get('a');
3465
4036
  return list.foldList(iFalse, function(el, next) {
3466
- return compEquals(el, needle, listType.of).branch(iTrue, next);
4037
+ return compEquals(el, needle, elType).branch(iTrue, next);
3467
4038
  });
3468
4039
  }
3469
4040
  },
3470
4041
  "empty?": {
3471
4042
  type: parse.type('empty? = [%] -> bool'),
3472
- impl: function(evalList) {
3473
- return evalList.map(function(list) {
3474
- return Value.boolean(list.length === 0);
3475
- });
3476
- },
3477
4043
  compile: function(list) {
3478
4044
  return list.foldList(iTrue, function() {
3479
4045
  return iFalse;
@@ -3482,40 +4048,10 @@ stdlib = Gibbon.stdlib = (function() {
3482
4048
  },
3483
4049
  weight: {
3484
4050
  type: parse.type('weight [numeric : numeric] = % -> numeric'),
3485
- impl: function(_, eWeights) {
3486
- return eWeights.then(function(weights) {
3487
- return combine(weights.elements).then(function(pairs) {
3488
- var denominator, numerator, p, process;
3489
- numerator = 0;
3490
- denominator = 0;
3491
- process = function(pair) {
3492
- return combine([pair.first, pair.second]).after(function(_arg) {
3493
- var first, second;
3494
- first = _arg[0], second = _arg[1];
3495
- numerator += first.value * second.value;
3496
- return denominator += second.value;
3497
- });
3498
- };
3499
- return combine((function() {
3500
- var _i, _len, _results;
3501
- _results = [];
3502
- for (_i = 0, _len = pairs.length; _i < _len; _i++) {
3503
- p = pairs[_i];
3504
- _results.push(process(p));
3505
- }
3506
- return _results;
3507
- })()).then(function() {
3508
- if (denominator === 0) {
3509
- return Promise.fail('cannot divide by zero');
3510
- }
3511
- return unit(Value.number(numerator / denominator));
3512
- });
3513
- });
3514
- });
3515
- },
3516
- compile: function(_, weights) {
3517
- var ratio;
3518
- return ratio = weights.foldList(IR.pair(IR.constant(0), IR.constant(0)), function(el, next) {
4051
+ compile: function(_, _arg) {
4052
+ var ratio, weights;
4053
+ weights = _arg[0];
4054
+ ratio = weights.foldList(IR.pair(IR.constant(0), IR.constant(0)), function(el, next) {
3519
4055
  var denominator, numerator, value, weight, weighted;
3520
4056
  value = el.depair('first');
3521
4057
  weight = el.depair('second');
@@ -3524,40 +4060,14 @@ stdlib = Gibbon.stdlib = (function() {
3524
4060
  weighted = value.binop('*', weight);
3525
4061
  return IR.pair(numerator.binop('+', weighted), denominator.binop('+', weight));
3526
4062
  });
4063
+ return ratio.depair('first').binop('/', ratio.depair('second'));
3527
4064
  }
3528
4065
  },
3529
4066
  filter: {
3530
4067
  type: parse.type('filter { %a -> bool } = [%a] -> [%a]'),
3531
- impl: function(evalInput, evalBlock) {
3532
- return evalBlock.then(function(block) {
3533
- var fn;
3534
- fn = block.fn;
3535
- return evalInput.then(function(list) {
3536
- var check, e, out;
3537
- out = [];
3538
- check = function(thunk) {
3539
- return fn(thunk).after(function(bool) {
3540
- if (bool.value) {
3541
- return out.push(thunk);
3542
- }
3543
- });
3544
- };
3545
- return combine((function() {
3546
- var _i, _len, _ref, _results;
3547
- _ref = list.elements;
3548
- _results = [];
3549
- for (_i = 0, _len = _ref.length; _i < _len; _i++) {
3550
- e = _ref[_i];
3551
- _results.push(check(e));
3552
- }
3553
- return _results;
3554
- })()).map(function() {
3555
- return Value.list(out);
3556
- });
3557
- });
3558
- });
3559
- },
3560
- compile: function(input, block) {
4068
+ compile: function(input, _arg) {
4069
+ var block;
4070
+ block = _arg[0];
3561
4071
  return input.filterList(function(el) {
3562
4072
  return block.app(el);
3563
4073
  });
@@ -3565,49 +4075,25 @@ stdlib = Gibbon.stdlib = (function() {
3565
4075
  },
3566
4076
  scale: {
3567
4077
  type: parse.type('scale (numeric:numeric) (numeric:numeric) = numeric -> numeric'),
3568
- impl: function(eInput, eDomain, eRange) {
3569
- return combine([eInput, eDomain, eRange]).then(function(_arg) {
3570
- var bounds, dom, input, range;
3571
- input = _arg[0], dom = _arg[1], range = _arg[2];
3572
- bounds = combine([dom.first, dom.second, range.first, range.second]);
3573
- return bounds.map(function(_arg1) {
3574
- var domHigh, domLow, domSize, rangeHigh, rangeLow, rangeSize, retranslated, scaled, translated;
3575
- domLow = _arg1[0], domHigh = _arg1[1], rangeLow = _arg1[2], rangeHigh = _arg1[3];
3576
- if (input.value < domLow.value) {
3577
- input = domLow;
3578
- } else if (input.value > domHigh.value) {
3579
- input = domHigh;
3580
- }
3581
- domSize = domHigh.value - domLow.value;
3582
- rangeSize = rangeHigh.value - rangeLow.value;
3583
- translated = input.value - domLow.value;
3584
- scaled = translated * rangeSize / domSize;
3585
- retranslated = scaled + rangeLow.value;
3586
- return Value.number(retranslated);
3587
- });
3588
- });
4078
+ compile: function(input, _arg) {
4079
+ var dom, domHigh, domLow, domSize, range, rangeHigh, rangeLow, rangeSize, retranslated, scaled, translated;
4080
+ dom = _arg[0], range = _arg[1];
4081
+ domLow = dom.depair('first');
4082
+ domHigh = dom.depair('second');
4083
+ rangeLow = range.depair('first');
4084
+ rangeHigh = range.depair('second');
4085
+ input = input.binop('<', domLow).branch(domLow, input);
4086
+ input = input.binop('>', domHigh).branch(domHigh, input);
4087
+ domSize = domHigh.binop('+', domLow.extern('-'));
4088
+ rangeSize = rangeHigh.binop('+', rangeLow.extern('-'));
4089
+ translated = input.binop('+', domLow.extern('-'));
4090
+ scaled = translated.binop('*', rangeSize.binop('/', domSize));
4091
+ retranslated = scaled.binop('+', rangeLow);
4092
+ return retranslated;
3589
4093
  }
3590
4094
  },
3591
4095
  map: {
3592
4096
  type: parse.type('map { %a -> %b } = [%a] -> [%b]'),
3593
- impl: function(evalList, evalBlock) {
3594
- return evalList.then(function(list) {
3595
- return evalBlock.map(function(block) {
3596
- var e, fn;
3597
- fn = block.fn;
3598
- return Value.list((function() {
3599
- var _i, _len, _ref, _results;
3600
- _ref = list.elements;
3601
- _results = [];
3602
- for (_i = 0, _len = _ref.length; _i < _len; _i++) {
3603
- e = _ref[_i];
3604
- _results.push(fn(e));
3605
- }
3606
- return _results;
3607
- })());
3608
- });
3609
- });
3610
- },
3611
4097
  compile: function(list, _arg) {
3612
4098
  var block;
3613
4099
  block = _arg[0];
@@ -3618,27 +4104,12 @@ stdlib = Gibbon.stdlib = (function() {
3618
4104
  },
3619
4105
  count: {
3620
4106
  type: parse.type('count = [%a] -> numeric'),
3621
- impl: function(evalList) {
3622
- return evalList.map(function(list) {
3623
- return Value.number(list.elements.length);
3624
- });
4107
+ compile: function(list) {
4108
+ return list.len();
3625
4109
  }
3626
4110
  },
3627
4111
  sum: {
3628
4112
  type: parse.type('sum = [numeric] -> numeric'),
3629
- impl: function(list) {
3630
- return list.then(function(val) {
3631
- return combine(val.elements).then(function(vals) {
3632
- var e, out, _i, _len;
3633
- out = 0;
3634
- for (_i = 0, _len = vals.length; _i < _len; _i++) {
3635
- e = vals[_i];
3636
- out += e.value;
3637
- }
3638
- return unit(Value.number(out));
3639
- });
3640
- });
3641
- },
3642
4113
  compile: function(list) {
3643
4114
  return list.foldList(IR.constant(0), function(el, next) {
3644
4115
  return el.binop('+', next);
@@ -3647,40 +4118,6 @@ stdlib = Gibbon.stdlib = (function() {
3647
4118
  },
3648
4119
  "case-sum": {
3649
4120
  type: parse.type('case-sum [bool : numeric] = % -> numeric'),
3650
- impl: function(_, evalList) {
3651
- return evalList.then(function(list) {
3652
- return combine(list.elements).then(function(elements) {
3653
- var first, numbers, second;
3654
- numbers = (function() {
3655
- var _i, _len, _ref, _results;
3656
- _results = [];
3657
- for (_i = 0, _len = elements.length; _i < _len; _i++) {
3658
- _ref = elements[_i], first = _ref.first, second = _ref.second;
3659
- _results.push((function(first, second) {
3660
- return first.then(function(cond) {
3661
- if (!cond.value) {
3662
- return unit(0);
3663
- }
3664
- return second.map(function(v) {
3665
- return v.value;
3666
- });
3667
- });
3668
- })(first, second));
3669
- }
3670
- return _results;
3671
- })();
3672
- return combine(numbers).then(function(nums) {
3673
- var num, out, _i, _len;
3674
- out = 0;
3675
- for (_i = 0, _len = nums.length; _i < _len; _i++) {
3676
- num = nums[_i];
3677
- out += num;
3678
- }
3679
- return unit(Value.number(out));
3680
- });
3681
- });
3682
- });
3683
- },
3684
4121
  compile: function(_, _arg) {
3685
4122
  var list;
3686
4123
  list = _arg[0];
@@ -3694,56 +4131,18 @@ stdlib = Gibbon.stdlib = (function() {
3694
4131
  },
3695
4132
  first: {
3696
4133
  type: parse.type('first = [%a] -> %a'),
3697
- impl: function(list) {
3698
- return list.then(function(val) {
3699
- return val.elements[0];
3700
- });
3701
- },
3702
4134
  compile: function(list) {
3703
- return list.delist(0);
4135
+ return list.delist(IR.constant(0));
3704
4136
  }
3705
4137
  },
3706
4138
  squish: {
3707
4139
  type: parse.type('squish = [%a] -> [%a]'),
3708
- impl: function(list) {
3709
- return list.then(function(_arg) {
3710
- var elements, promise, wrapped;
3711
- elements = _arg.elements;
3712
- wrapped = (function() {
3713
- var _i, _len, _results;
3714
- _results = [];
3715
- for (_i = 0, _len = elements.length; _i < _len; _i++) {
3716
- promise = elements[_i];
3717
- _results.push(promise.then(function(e) {
3718
- return e.resolve().map(function(e) {
3719
- return [e.promise()];
3720
- });
3721
- }).or(unit([])));
3722
- }
3723
- return _results;
3724
- })();
3725
- return combine(wrapped).map(function(lists) {
3726
- return Value.list(catLists(lists));
3727
- });
3728
- });
3729
- },
3730
4140
  compile: function(list) {
3731
- return list.filterList(function(el) {
3732
- return IR.rescue(el.seq('v', function(_) {
3733
- return iTrue;
3734
- }), iFalse);
3735
- });
4141
+ return list.squishList();
3736
4142
  }
3737
4143
  },
3738
4144
  add: {
3739
4145
  type: parse.type('add numeric = numeric -> numeric'),
3740
- impl: function(input, num) {
3741
- return combine([input, num]).map(function(_arg) {
3742
- var lhs, rhs;
3743
- lhs = _arg[0], rhs = _arg[1];
3744
- return Value.number(lhs.value + rhs.value);
3745
- });
3746
- },
3747
4146
  compile: function(input, _arg) {
3748
4147
  var num;
3749
4148
  num = _arg[0];
@@ -3752,13 +4151,6 @@ stdlib = Gibbon.stdlib = (function() {
3752
4151
  },
3753
4152
  sub: {
3754
4153
  type: parse.type('sub numeric = numeric -> numeric'),
3755
- impl: function(input, num) {
3756
- return combine([input, num]).map(function(_arg) {
3757
- var lhs, rhs;
3758
- lhs = _arg[0], rhs = _arg[1];
3759
- return Value.number(lhs.value - rhs.value);
3760
- });
3761
- },
3762
4154
  compile: function(input, _arg) {
3763
4155
  var num;
3764
4156
  num = _arg[0];
@@ -3767,42 +4159,24 @@ stdlib = Gibbon.stdlib = (function() {
3767
4159
  },
3768
4160
  id: {
3769
4161
  type: parse.type('id = %a -> %a'),
3770
- impl: function(x) {
3771
- return x;
3772
- },
3773
4162
  compile: function(input) {
3774
4163
  return input;
3775
4164
  }
3776
4165
  },
3777
4166
  "else": {
3778
4167
  type: parse.type('else = % -> bool'),
3779
- impl: function(_) {
3780
- return unit(vTrue);
3781
- },
3782
4168
  compile: function(_) {
3783
4169
  return iTrue;
3784
4170
  }
3785
4171
  },
3786
4172
  not: {
3787
4173
  type: parse.type('not = bool -> bool'),
3788
- impl: function(evalBool) {
3789
- return evalBool.map(function(bool) {
3790
- return Value.boolean(!bool.value);
3791
- });
3792
- },
3793
4174
  compile: function(input) {
3794
4175
  return input.extern('!');
3795
4176
  }
3796
4177
  },
3797
4178
  gt: {
3798
4179
  type: parse.type('gt numeric = numeric -> bool'),
3799
- impl: function(input, num) {
3800
- return combine([input, num]).map(function(_arg) {
3801
- var lhs, rhs;
3802
- lhs = _arg[0], rhs = _arg[1];
3803
- return Value.boolean(lhs.value > rhs.value);
3804
- });
3805
- },
3806
4180
  compile: function(input, _arg) {
3807
4181
  var num;
3808
4182
  num = _arg[0];
@@ -3811,13 +4185,6 @@ stdlib = Gibbon.stdlib = (function() {
3811
4185
  },
3812
4186
  lt: {
3813
4187
  type: parse.type('lt numeric = numeric -> bool'),
3814
- impl: function(input, num) {
3815
- return combine([input, num]).map(function(_arg) {
3816
- var lhs, rhs;
3817
- lhs = _arg[0], rhs = _arg[1];
3818
- return Value.boolean(lhs.value < rhs.value);
3819
- });
3820
- },
3821
4188
  compile: function(input, _arg) {
3822
4189
  var num;
3823
4190
  num = _arg[0];
@@ -3826,18 +4193,10 @@ stdlib = Gibbon.stdlib = (function() {
3826
4193
  },
3827
4194
  eq: {
3828
4195
  type: parse.type('eq %a = %a -> bool'),
3829
- impl: function(input, obj) {
3830
- return combine([input, obj]).then(function(_arg) {
3831
- var lhs, rhs;
3832
- lhs = _arg[0], rhs = _arg[1];
3833
- return lhs.equals(rhs);
3834
- });
3835
- },
3836
- compile: function(input, _arg, _, _arg1) {
3837
- var obj, objType;
4196
+ compile: function(input, _arg, tvars) {
4197
+ var obj;
3838
4198
  obj = _arg[0];
3839
- objType = _arg1[0];
3840
- return compEquals(input, obj, objType);
4199
+ return compEquals(input, obj, tvars.get('a'));
3841
4200
  }
3842
4201
  }
3843
4202
  };
@@ -3845,7 +4204,7 @@ stdlib = Gibbon.stdlib = (function() {
3845
4204
  // Generated by CoffeeScript 1.6.3
3846
4205
  Gibbon.jsonConsumer = (function() {
3847
4206
  return function(tables) {
3848
- var analyzeList, getType, getValue, lists;
4207
+ var analyzeList, getType, getValue, listLookup, lists;
3849
4208
  getType = function(id, accessorName, t, callback) {
3850
4209
  var fields;
3851
4210
  if (!tables.hasOwnProperty(id)) {
@@ -3895,6 +4254,15 @@ Gibbon.jsonConsumer = (function() {
3895
4254
  }
3896
4255
  });
3897
4256
  };
4257
+ listLookup = function(id, listName, v, callback) {
4258
+ var list;
4259
+ list = lists[listName].values;
4260
+ if (list.indexOf(id) >= 0) {
4261
+ return callback(null, v.boolean(true));
4262
+ } else {
4263
+ return callback(null, v.boolean(false));
4264
+ }
4265
+ };
3898
4266
  return {
3899
4267
  analyzeQuery: function(id, query, t, callback) {
3900
4268
  switch (query.type) {
@@ -3907,7 +4275,11 @@ Gibbon.jsonConsumer = (function() {
3907
4275
  }
3908
4276
  },
3909
4277
  performQuery: function(id, annotations, v, callback) {
3910
- return getValue(id, annotations, v, callback);
4278
+ if ('list' in annotations) {
4279
+ return listLookup(id, annotations.list, v, callback);
4280
+ } else {
4281
+ return getValue(id, annotations, v, callback);
4282
+ }
3911
4283
  }
3912
4284
  };
3913
4285
  };