goodguide-gibbon 0.5.1 → 0.6.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.
@@ -359,14 +359,13 @@ Parsimmon.Parser = P(function(_, _super, Parser) {
359
359
  return Parsimmon;
360
360
  })()
361
361
  // Generated by CoffeeScript 1.6.3
362
- var Gibbon;
363
-
364
- Gibbon = {};
365
- // Generated by CoffeeScript 1.6.3
366
- var CompMap, DEBUG, Hash, Map, Union, allocate, asyncMap, catLists, contIter, contMap, genId, isArray, uniq, _ref,
362
+ var AST, CachingPromise, CompMap, CompiledCode, ContinuationTrace, Core, DEBUG, Dependency, Failure, Gibbon, Hash, JS, List, Map, ObjHash, Promise, RVal, Semantic, Step, Thunk, Trace, Type, TypeAST, TypeExpr, TypeLookup, UnitPromise, Value, VarTrace, Variant, allocate, analyze, applyOp1, applyOp2, asyncMap, catLists, contIter, contMap, equalArrays, eval_, genId, isArray, nameGen, parse, stdlib, uniq, _ref, _ref1, _ref10, _ref11, _ref12, _ref13, _ref14, _ref15, _ref16, _ref17, _ref18, _ref19, _ref2, _ref3, _ref4, _ref5, _ref6, _ref7, _ref8, _ref9,
367
363
  __slice = [].slice,
368
364
  __hasProp = {}.hasOwnProperty,
369
- __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; };
365
+ __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; },
366
+ __indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; };
367
+
368
+ Gibbon = {};
370
369
 
371
370
  genId = (function() {
372
371
  var id;
@@ -376,6 +375,22 @@ genId = (function() {
376
375
  };
377
376
  })();
378
377
 
378
+ Thunk = (function() {
379
+ Thunk.trampoline = function(t) {
380
+ while (t instanceof Thunk) {
381
+ t = t.run();
382
+ }
383
+ return t;
384
+ };
385
+
386
+ function Thunk(run) {
387
+ this.run = run;
388
+ }
389
+
390
+ return Thunk;
391
+
392
+ })();
393
+
379
394
 
380
395
 
381
396
 
@@ -398,6 +413,53 @@ catLists = function(lists) {
398
413
  return out;
399
414
  };
400
415
 
416
+ equalArrays = function(a1, a2) {
417
+ var e, i, _i, _len;
418
+ for (i = _i = 0, _len = a1.length; _i < _len; i = ++_i) {
419
+ e = a1[i];
420
+ if (e !== a2[i]) {
421
+ return false;
422
+ }
423
+ }
424
+ return true;
425
+ };
426
+
427
+ applyOp1 = function(op, arg) {
428
+ switch (op) {
429
+ case '!':
430
+ return !arg;
431
+ case '-':
432
+ return -arg;
433
+ default:
434
+ throw "unknown operator " + op;
435
+ }
436
+ };
437
+
438
+ applyOp2 = function(op, l, r) {
439
+ switch (op) {
440
+ case '+':
441
+ return l + r;
442
+ case '*':
443
+ return l * r;
444
+ case '/':
445
+ return l / r;
446
+ case '%':
447
+ return l % r;
448
+ case '===':
449
+ return l === r;
450
+ case '<':
451
+ return l < r;
452
+ case '>':
453
+ return l > r;
454
+ case '<=':
455
+ return l <= r;
456
+ case '>=':
457
+ return l >= r;
458
+ default:
459
+ throw "unknown operator " + op;
460
+ }
461
+ };
462
+
401
463
  uniq = function(list, eq) {
402
464
  var el, out, u, _i, _j, _len, _len1;
403
465
  if (eq == null) {
@@ -427,34 +489,34 @@ uniq = function(list, eq) {
427
489
 
428
490
  allocate = Object.create || function(proto) {
429
491
  var dummy;
430
- dummy = function() {};
492
+ dummy = (function() {});
431
493
  dummy.prototype = proto;
432
494
  return new dummy;
433
495
  };
434
496
 
435
497
  asyncMap = function(list, mapper, cb) {
436
- var el, i, output, response, seen, _i, _len, _results;
498
+ var el, i, output, response, retVal, seen, _i, _len;
437
499
  if (list.length === 0) {
438
500
  return cb(list);
439
501
  }
440
502
  seen = 0;
441
503
  output = [];
504
+ retVal = null;
442
505
  response = function(i) {
443
506
  return function(el) {
444
507
  seen += 1;
445
508
  output[i] = el;
446
509
  if (seen >= list.length) {
447
- cb(output);
510
+ retVal = cb(output);
448
511
  }
449
512
  return null;
450
513
  };
451
514
  };
452
- _results = [];
453
515
  for (i = _i = 0, _len = list.length; _i < _len; i = ++_i) {
454
516
  el = list[i];
455
- _results.push(mapper(el, response(i), i));
517
+ mapper(el, response(i), i);
456
518
  }
457
- return _results;
519
+ return retVal;
458
520
  };
459
521
 
460
522
  contMap = function(list, mapper, cb) {
@@ -484,10 +546,12 @@ contIter = function(list, f, cb) {
484
546
  return loop_(0);
485
547
  };
486
548
 
487
- Union = (function() {
549
+ Variant = (function() {
488
550
  var castJSON;
489
551
 
490
- Union.specialize = function(tag, names) {
552
+ Variant.prototype.__isVariant__ = true;
553
+
554
+ Variant.specialize = function(tag, names) {
491
555
  var klass, name, subclass, _i, _len;
492
556
  klass = this;
493
557
  subclass = function(values) {
@@ -509,19 +573,20 @@ Union = (function() {
509
573
  };
510
574
  };
511
575
 
512
- Union.types = function(tags) {
576
+ Variant.variants = function(tagSpec) {
513
577
  var names, tag, _results;
514
- this.tags = tags;
578
+ this.prototype._type = this.name;
579
+ this.tags = tagSpec;
515
580
  _results = [];
516
- for (tag in tags) {
517
- if (!__hasProp.call(tags, tag)) continue;
518
- names = tags[tag];
581
+ for (tag in tagSpec) {
582
+ if (!__hasProp.call(tagSpec, tag)) continue;
583
+ names = tagSpec[tag];
519
584
  _results.push(this[tag] = this.specialize(tag, names));
520
585
  }
521
586
  return _results;
522
587
  };
523
588
 
524
- function Union(_names, _values) {
589
+ function Variant(_names, _values) {
525
590
  var i, name, _i, _len, _ref;
526
591
  this._names = _names;
527
592
  this._values = _values;
@@ -532,7 +597,7 @@ Union = (function() {
532
597
  }
533
598
  }
534
599
 
535
- Union.prototype.cases = function(cases) {
600
+ Variant.prototype.cases = function(cases) {
536
601
  var fn;
537
602
  fn = cases[this._tag] || cases.other;
538
603
  if (!fn) {
@@ -557,11 +622,12 @@ Union = (function() {
557
622
  }
558
623
  };
559
624
 
560
- Union.prototype.asJSON = function() {
625
+ Variant.prototype.asJSON = function() {
561
626
  var name, out, _i, _len, _ref;
562
627
  out = {
628
+ __isVariant__: true,
563
629
  _tag: this._tag,
564
- _union: this.constructor.name
630
+ _type: this.constructor.name
565
631
  };
566
632
  _ref = this._names;
567
633
  for (_i = 0, _len = _ref.length; _i < _len; _i++) {
@@ -571,7 +637,7 @@ Union = (function() {
571
637
  return out;
572
638
  };
573
639
 
574
- Union.fromJSON = function(o) {
640
+ Variant.fromJSON = function(o) {
575
641
  var constructor, e, name, names, vals;
576
642
  if (isArray(o)) {
577
643
  return (function() {
@@ -587,7 +653,7 @@ Union = (function() {
587
653
  if (!(typeof o === 'object' && (o != null) && '_tag' in o)) {
588
654
  return o;
589
655
  }
590
- constructor = Gibbon[o._union];
656
+ constructor = Gibbon[o._type];
591
657
  names = constructor.tags[o._tag];
592
658
  vals = (function() {
593
659
  var _i, _len, _results;
@@ -601,14 +667,14 @@ Union = (function() {
601
667
  return constructor[o._tag].apply(constructor, vals);
602
668
  };
603
669
 
604
- Union.wrap = function(o) {
670
+ Variant.wrap = function(o) {
605
671
  if (o instanceof this) {
606
672
  return o;
607
673
  }
608
674
  return this.fromJSON(o);
609
675
  };
610
676
 
611
- return Union;
677
+ return Variant;
612
678
 
613
679
  })();
614
680
 
@@ -693,7 +759,7 @@ Map = (function() {
693
759
  })();
694
760
 
695
761
  Gibbon.Hash = Hash = (function(_super) {
696
- var salt;
762
+ var salt, saltLen;
697
763
 
698
764
  __extends(Hash, _super);
699
765
 
@@ -706,13 +772,15 @@ Gibbon.Hash = Hash = (function(_super) {
706
772
 
707
773
  salt = '<key>';
708
774
 
775
+ saltLen = salt.length;
776
+
709
777
  Hash.prototype.loadKey = function(k) {
710
778
  return salt + k;
711
779
  };
712
780
 
713
781
  Hash.prototype.dumpKey = function(k) {
714
782
  if (k.indexOf(salt) === 0) {
715
- return k.slice(salt.length);
783
+ return k.slice(saltLen);
716
784
  }
717
785
  };
718
786
 
@@ -734,10 +802,9 @@ Gibbon.Hash = Hash = (function(_super) {
734
802
  for (k in this) {
735
803
  if (!__hasProp.call(this, k)) continue;
736
804
  v = this[k];
737
- if (k.indexOf(salt) !== 0) {
738
- continue;
805
+ if (k.slice(0, saltLen) === salt) {
806
+ _results.push(f(k.slice(saltLen), v));
739
807
  }
740
- _results.push(f(k.slice(salt.length), v));
741
808
  }
742
809
  return _results;
743
810
  };
@@ -746,6 +813,45 @@ Gibbon.Hash = Hash = (function(_super) {
746
813
 
747
814
  })(Map);
748
815
 
816
+ Gibbon.ObjHash = ObjHash = (function(_super) {
817
+ __extends(ObjHash, _super);
818
+
819
+ function ObjHash() {
820
+ _ref1 = ObjHash.__super__.constructor.apply(this, arguments);
821
+ return _ref1;
822
+ }
823
+
824
+ ObjHash.prototype.get = function(k) {
825
+ var _ref2;
826
+ return (_ref2 = this[k.hash()]) != null ? _ref2.value : void 0;
827
+ };
828
+
829
+ ObjHash.prototype.set = function(k, v) {
830
+ return this[k.hash()] = {
831
+ key: k,
832
+ value: v
833
+ };
834
+ };
835
+
836
+ ObjHash.prototype.has = function(k) {
837
+ return this.hasOwnProperty(k.hash());
838
+ };
839
+
840
+ ObjHash.prototype.each = function(f) {
841
+ var k, v, _results;
842
+ _results = [];
843
+ for (k in this) {
844
+ if (!__hasProp.call(this, k)) continue;
845
+ v = this[k];
846
+ _results.push(f(v.key, v.value));
847
+ }
848
+ return _results;
849
+ };
850
+
851
+ return ObjHash;
852
+
853
+ })(Hash);
854
+
749
855
  CompMap = (function(_super) {
750
856
  __extends(CompMap, _super);
751
857
 
@@ -768,10 +874,10 @@ CompMap = (function(_super) {
768
874
  };
769
875
 
770
876
  CompMap.prototype.keyIndex = function(k) {
771
- var i, key, _i, _len, _ref1;
772
- _ref1 = this.keys;
773
- for (i = _i = 0, _len = _ref1.length; _i < _len; i = ++_i) {
774
- key = _ref1[i];
877
+ var i, key, _i, _len, _ref2;
878
+ _ref2 = this.keys;
879
+ for (i = _i = 0, _len = _ref2.length; _i < _len; i = ++_i) {
880
+ key = _ref2[i];
775
881
  if (this.compare(key, k)) {
776
882
  return i;
777
883
  }
@@ -800,37 +906,37 @@ CompMap = (function(_super) {
800
906
  };
801
907
 
802
908
  CompMap.prototype.each = function(f) {
803
- var i, k, _i, _len, _ref1, _results;
804
- _ref1 = this.keys;
909
+ var i, k, _i, _len, _ref2, _results;
910
+ _ref2 = this.keys;
805
911
  _results = [];
806
- for (i = _i = 0, _len = _ref1.length; _i < _len; i = ++_i) {
807
- k = _ref1[i];
912
+ for (i = _i = 0, _len = _ref2.length; _i < _len; i = ++_i) {
913
+ k = _ref2[i];
808
914
  _results.push(f(k, this.values[i]));
809
915
  }
810
916
  return _results;
811
917
  };
812
918
 
813
919
  CompMap.prototype.eachAsync = function(f, cb) {
814
- var i, k, keys, responder, seen, _i, _len, _results;
920
+ var i, k, keys, output, responder, seen, _i, _len;
815
921
  keys = this.keys;
816
922
  if (keys.length === 0) {
817
923
  return;
818
924
  }
925
+ output = null;
819
926
  seen = 0;
820
927
  responder = function(i) {
821
928
  return function() {
822
929
  seen += 1;
823
930
  if (seen >= keys.length) {
824
- return cb();
931
+ return output = cb();
825
932
  }
826
933
  };
827
934
  };
828
- _results = [];
829
935
  for (i = _i = 0, _len = keys.length; _i < _len; i = ++_i) {
830
936
  k = keys[i];
831
- _results.push(f(k, this.values[i], responder(i)));
937
+ f(k, this.values[i], responder(i));
832
938
  }
833
- return _results;
939
+ return output;
834
940
  };
835
941
 
836
942
  CompMap.prototype.fetch = function(k, f) {
@@ -863,10 +969,48 @@ CompMap = (function(_super) {
863
969
  return CompMap;
864
970
 
865
971
  })(Map);
866
- // Generated by CoffeeScript 1.6.3
867
- var AST, TypeAST, parse, _ref, _ref1,
868
- __hasProp = {}.hasOwnProperty,
869
- __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; };
972
+
973
+ List = (function(_super) {
974
+ __extends(List, _super);
975
+
976
+ function List() {
977
+ _ref2 = List.__super__.constructor.apply(this, arguments);
978
+ return _ref2;
979
+ }
980
+
981
+ List.variants({
982
+ empty: [],
983
+ cons: ['head', 'tail']
984
+ });
985
+
986
+ List.single = function(el) {
987
+ return List.empty().cons(el);
988
+ };
989
+
990
+ List.prototype.cons = function(el) {
991
+ return List.cons(el, this);
992
+ };
993
+
994
+ List.prototype.toArray = function() {
995
+ return this.mapArray(function(x) {
996
+ return x;
997
+ });
998
+ };
999
+
1000
+ List.prototype.mapArray = function(f) {
1001
+ var out, tr;
1002
+ out = [];
1003
+ tr = this;
1004
+ while (tr._tag === 'cons') {
1005
+ out.push(f(tr.head));
1006
+ tr = tr.tail;
1007
+ }
1008
+ return out;
1009
+ };
1010
+
1011
+ return List;
1012
+
1013
+ })(Variant);
870
1014
 
871
1015
  Gibbon.AST = AST = (function(_super) {
872
1016
  var inspectDefinitions;
@@ -874,11 +1018,11 @@ Gibbon.AST = AST = (function(_super) {
874
1018
  __extends(AST, _super);
875
1019
 
876
1020
  function AST() {
877
- _ref = AST.__super__.constructor.apply(this, arguments);
878
- return _ref;
1021
+ _ref3 = AST.__super__.constructor.apply(this, arguments);
1022
+ return _ref3;
879
1023
  }
880
1024
 
881
- AST.types({
1025
+ AST.variants({
882
1026
  integer: ['value'],
883
1027
  decimal: ['value'],
884
1028
  percent: ['value'],
@@ -1006,17 +1150,17 @@ Gibbon.AST = AST = (function(_super) {
1006
1150
 
1007
1151
  return AST;
1008
1152
 
1009
- })(Union);
1153
+ })(Variant);
1010
1154
 
1011
1155
  Gibbon.TypeAST = TypeAST = (function(_super) {
1012
1156
  __extends(TypeAST, _super);
1013
1157
 
1014
1158
  function TypeAST() {
1015
- _ref1 = TypeAST.__super__.constructor.apply(this, arguments);
1016
- return _ref1;
1159
+ _ref4 = TypeAST.__super__.constructor.apply(this, arguments);
1160
+ return _ref4;
1017
1161
  }
1018
1162
 
1019
- TypeAST.types({
1163
+ TypeAST.variants({
1020
1164
  concrete: ['name'],
1021
1165
  variable: ['name'],
1022
1166
  "native": ['id'],
@@ -1038,10 +1182,10 @@ Gibbon.TypeAST = TypeAST = (function(_super) {
1038
1182
 
1039
1183
  return TypeAST;
1040
1184
 
1041
- })(Union);
1185
+ })(Variant);
1042
1186
 
1043
1187
  parse = Gibbon.parse = (function() {
1044
- var accessor, accessorExpr, arrow, arrowType, assertString, bang, blankLines, blockExpr, blockType, comma, commaSepFlows, comment, component, concrete, decimal, decimalExpr, defaulted, define, definition, expr, fail, flow, fraction, fractionExpr, frame, freeFrame, fullSignature, func, funcPlaceholder, identifier, innerFrame, integer, integerExpr, isString, label, labelVal, lbrace, lbrack, lexeme, lines, listExpr, listType, lparen, lsplat, metadata, multiline, name, nativeId, nativeType, nonDefaultedFlow, nonPairedFlow, numericExpr, pair, parenFlow, parenFrame, parenType, percent, percentExpr, program, query, queryExpr, rbrace, rbrack, regex, rparen, rsplat, signature, simpleType, singletonFlow, squishListExpr, str, string, stringExpr, substExpr, succeed, tag, tassign, type, typeVar, variable, whitespace, wildType, wildcard;
1188
+ var accessor, accessorExpr, arrow, arrowType, assertString, bang, blankLines, blockExpr, blockType, comma, commaSepFlows, comment, component, concrete, decimal, decimalExpr, defaulted, define, definition, expr, fail, flow, fraction, fractionExpr, frame, freeFrame, fullSignature, func, funcPlaceholder, identifier, innerFrame, integer, integerExpr, isString, label, labelVal, lbrace, lbrack, lexeme, lines, listExpr, listType, lparen, lsplat, metadata, multiline, name, nativeId, nativeType, nonDefaultedFlow, nonPairedFlow, numericExpr, pair, parenFlow, parenFrame, parenType, percent, percentExpr, program, query, queryArg, queryExpr, rbrace, rbrack, regex, rparen, rsplat, signature, simpleType, singletonFlow, squishListExpr, str, string, stringExpr, substExpr, succeed, tag, tassign, type, typeVar, variable, whitespace, wildType, wildcard;
1045
1189
  tag = function(name, parser) {
1046
1190
  return parser.tag(name);
1047
1191
  };
@@ -1079,6 +1223,7 @@ parse = Gibbon.parse = (function() {
1079
1223
  lsplat = multiline(string('[*'));
1080
1224
  rsplat = lexeme(string('*]'));
1081
1225
  query = lexeme(string('.').then(identifier));
1226
+ queryArg = lexeme(regex(/^[\w-]+/));
1082
1227
  accessor = lexeme(string('@').then(identifier));
1083
1228
  name = lexeme(identifier);
1084
1229
  str = lexeme(string("'").then(regex(/^[^']*/)).skip(string("'")));
@@ -1101,8 +1246,8 @@ parse = Gibbon.parse = (function() {
1101
1246
  return AST.percent(parseInt(p));
1102
1247
  });
1103
1248
  fractionExpr = fraction.map(function(f) {
1104
- var denom, num, _ref2;
1105
- _ref2 = f.split('/'), num = _ref2[0], denom = _ref2[1];
1249
+ var denom, num, _ref5;
1250
+ _ref5 = f.split('/'), num = _ref5[0], denom = _ref5[1];
1106
1251
  return AST.fraction(num, denom);
1107
1252
  });
1108
1253
  stringExpr = str.map(function(s) {
@@ -1112,7 +1257,7 @@ parse = Gibbon.parse = (function() {
1112
1257
  return AST.query('access', [name]);
1113
1258
  });
1114
1259
  queryExpr = query.then(function(q) {
1115
- return name.many().map(function(args) {
1260
+ return queryArg.many().map(function(args) {
1116
1261
  return AST.query(q, args);
1117
1262
  });
1118
1263
  });
@@ -1263,21 +1408,16 @@ parse = Gibbon.parse = (function() {
1263
1408
  };
1264
1409
  return parse;
1265
1410
  })();
1266
- // Generated by CoffeeScript 1.6.3
1267
- var Semantic, Type, TypeExpr, TypeLookup, analyze, _ref, _ref1, _ref2, _ref3,
1268
- __hasProp = {}.hasOwnProperty,
1269
- __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; },
1270
- __slice = [].slice;
1271
1411
 
1272
1412
  Gibbon.Semantic = Semantic = (function(_super) {
1273
1413
  __extends(Semantic, _super);
1274
1414
 
1275
1415
  function Semantic() {
1276
- _ref = Semantic.__super__.constructor.apply(this, arguments);
1277
- return _ref;
1416
+ _ref5 = Semantic.__super__.constructor.apply(this, arguments);
1417
+ return _ref5;
1278
1418
  }
1279
1419
 
1280
- Semantic.types({
1420
+ Semantic.variants({
1281
1421
  definition: ['dependencies', 'flow'],
1282
1422
  literal: ['syntax'],
1283
1423
  query: ['annotations'],
@@ -1291,37 +1431,127 @@ Gibbon.Semantic = Semantic = (function(_super) {
1291
1431
  defaulted: ['body', 'alternative']
1292
1432
  });
1293
1433
 
1434
+ Semantic.prototype.inspect = function() {
1435
+ return this.cases({
1436
+ definition: function(deps, flow) {
1437
+ var d;
1438
+ return "<" + (((function() {
1439
+ var _i, _len, _results;
1440
+ _results = [];
1441
+ for (_i = 0, _len = deps.length; _i < _len; _i++) {
1442
+ d = deps[_i];
1443
+ _results.push(d.inspect());
1444
+ }
1445
+ return _results;
1446
+ })()).join(', ')) + "> " + (flow.inspect());
1447
+ },
1448
+ literal: function(syntax) {
1449
+ return syntax.inspect();
1450
+ },
1451
+ query: function(annotations) {
1452
+ return "(? " + (JSON.stringify(annotations)) + ")";
1453
+ },
1454
+ localAccessor: function(name) {
1455
+ return "@" + name;
1456
+ },
1457
+ pair: function(first, second) {
1458
+ return "(" + (first.inspect()) + " : " + (second.inspect()) + ")";
1459
+ },
1460
+ block: function(body) {
1461
+ return "{ " + (body.inspect()) + " }";
1462
+ },
1463
+ list: function(elements, squish) {
1464
+ var e, l, r;
1465
+ if (squish) {
1466
+ l = "[*";
1467
+ r = "*]";
1468
+ } else {
1469
+ l = "[";
1470
+ r = "]";
1471
+ }
1472
+ return "" + l + (((function() {
1473
+ var _i, _len, _results;
1474
+ _results = [];
1475
+ for (_i = 0, _len = elements.length; _i < _len; _i++) {
1476
+ e = elements[_i];
1477
+ _results.push(e.inspect());
1478
+ }
1479
+ return _results;
1480
+ })()).join(', ')) + r;
1481
+ },
1482
+ flow: function(type, head, tail) {
1483
+ if (tail) {
1484
+ return "" + (tail.inspect()) + " -> " + (head.inspect()) + " :: " + (type.inspect());
1485
+ } else {
1486
+ return "" + (head.inspect()) + " :: " + (type.inspect());
1487
+ }
1488
+ },
1489
+ func: function(name, args, scope) {
1490
+ var a;
1491
+ return "" + name + " " + (((function() {
1492
+ var _i, _len, _results;
1493
+ _results = [];
1494
+ for (_i = 0, _len = args.length; _i < _len; _i++) {
1495
+ a = args[_i];
1496
+ _results.push("(" + (a.inspect()) + ")");
1497
+ }
1498
+ return _results;
1499
+ })()).join(' '));
1500
+ },
1501
+ subst: function(flow) {
1502
+ return flow.inspect();
1503
+ },
1504
+ defaulted: function(body, alt) {
1505
+ return "" + (body.inspect()) + " | " + (alt.inspect());
1506
+ }
1507
+ });
1508
+ };
1509
+
1294
1510
  return Semantic;
1295
1511
 
1296
- })(Union);
1512
+ })(Variant);
1297
1513
 
1298
1514
  Gibbon.TypeLookup = TypeLookup = (function(_super) {
1299
1515
  __extends(TypeLookup, _super);
1300
1516
 
1301
1517
  function TypeLookup() {
1302
- _ref1 = TypeLookup.__super__.constructor.apply(this, arguments);
1303
- return _ref1;
1518
+ _ref6 = TypeLookup.__super__.constructor.apply(this, arguments);
1519
+ return _ref6;
1304
1520
  }
1305
1521
 
1306
- TypeLookup.types({
1522
+ TypeLookup.variants({
1307
1523
  response: ['query', 'analysis'],
1308
1524
  local: ['name'],
1309
1525
  error: ['error']
1310
1526
  });
1311
1527
 
1528
+ TypeLookup.prototype.inspect = function() {
1529
+ return this.cases({
1530
+ response: function(query, analysis) {
1531
+ return "" + (query.inspect()) + (JSON.stringify(analysis.annotations)) + "::" + (analysis.type.inspect());
1532
+ },
1533
+ local: function(name) {
1534
+ return "@" + name;
1535
+ },
1536
+ error: function(e) {
1537
+ return "!" + e;
1538
+ }
1539
+ });
1540
+ };
1541
+
1312
1542
  return TypeLookup;
1313
1543
 
1314
- })(Union);
1544
+ })(Variant);
1315
1545
 
1316
1546
  Gibbon.Type = Type = (function(_super) {
1317
1547
  __extends(Type, _super);
1318
1548
 
1319
1549
  function Type() {
1320
- _ref2 = Type.__super__.constructor.apply(this, arguments);
1321
- return _ref2;
1550
+ _ref7 = Type.__super__.constructor.apply(this, arguments);
1551
+ return _ref7;
1322
1552
  }
1323
1553
 
1324
- Type.types({
1554
+ Type.variants({
1325
1555
  block: ['from', 'to'],
1326
1556
  pair: ['first', 'second'],
1327
1557
  list: ['of'],
@@ -1355,17 +1585,17 @@ Gibbon.Type = Type = (function(_super) {
1355
1585
 
1356
1586
  return Type;
1357
1587
 
1358
- })(Union);
1588
+ })(Variant);
1359
1589
 
1360
1590
  Gibbon.TypeExpr = TypeExpr = (function(_super) {
1361
1591
  __extends(TypeExpr, _super);
1362
1592
 
1363
1593
  function TypeExpr() {
1364
- _ref3 = TypeExpr.__super__.constructor.apply(this, arguments);
1365
- return _ref3;
1594
+ _ref8 = TypeExpr.__super__.constructor.apply(this, arguments);
1595
+ return _ref8;
1366
1596
  }
1367
1597
 
1368
- TypeExpr.types({
1598
+ TypeExpr.variants({
1369
1599
  expr: ['expr'],
1370
1600
  variable: ['name'],
1371
1601
  query: ['input', 'scope', 'query'],
@@ -1404,6 +1634,18 @@ Gibbon.TypeExpr = TypeExpr = (function(_super) {
1404
1634
  expr: function(e) {
1405
1635
  return "<" + (e.inspect()) + ">";
1406
1636
  },
1637
+ error: function(t, args) {
1638
+ var a;
1639
+ return "?" + t + "(" + (((function() {
1640
+ var _i, _len, _results;
1641
+ _results = [];
1642
+ for (_i = 0, _len = args.length; _i < _len; _i++) {
1643
+ a = args[_i];
1644
+ _results.push(a.inspect());
1645
+ }
1646
+ return _results;
1647
+ })()).join(' ')) + ")";
1648
+ },
1407
1649
  variable: function(name) {
1408
1650
  return "%" + name;
1409
1651
  },
@@ -1432,6 +1674,18 @@ Gibbon.TypeExpr = TypeExpr = (function(_super) {
1432
1674
  })();
1433
1675
  return "(" + name + " " + (exprs.join(' ')) + ")";
1434
1676
  },
1677
+ error: function(t, args) {
1678
+ var a;
1679
+ return "?" + t + "(" + (((function() {
1680
+ var _i, _len, _results;
1681
+ _results = [];
1682
+ for (_i = 0, _len = args.length; _i < _len; _i++) {
1683
+ a = args[_i];
1684
+ _results.push(a.inspect());
1685
+ }
1686
+ return _results;
1687
+ })()).join(' ')) + ")";
1688
+ },
1435
1689
  any: function() {
1436
1690
  return '*';
1437
1691
  }
@@ -1529,11 +1783,11 @@ Gibbon.TypeExpr = TypeExpr = (function(_super) {
1529
1783
  return TypeExpr["native"](t.id);
1530
1784
  }
1531
1785
  return TypeExpr.param(t._tag, (function() {
1532
- var _i, _len, _ref4, _results;
1533
- _ref4 = t._values;
1786
+ var _i, _len, _ref9, _results;
1787
+ _ref9 = t._values;
1534
1788
  _results = [];
1535
- for (_i = 0, _len = _ref4.length; _i < _len; _i++) {
1536
- st = _ref4[_i];
1789
+ for (_i = 0, _len = _ref9.length; _i < _len; _i++) {
1790
+ st = _ref9[_i];
1537
1791
  _results.push(this.fromType(st));
1538
1792
  }
1539
1793
  return _results;
@@ -1591,7 +1845,7 @@ Gibbon.TypeExpr = TypeExpr = (function(_super) {
1591
1845
 
1592
1846
  return TypeExpr;
1593
1847
 
1594
- })(Union);
1848
+ })(Variant);
1595
1849
 
1596
1850
  analyze = Gibbon.analyze = (function() {
1597
1851
  var generate, solve;
@@ -1606,9 +1860,9 @@ analyze = Gibbon.analyze = (function() {
1606
1860
  NativeContext.prototype.query = function(id, query, cb) {
1607
1861
  return this.externalLookup.call(null, id, query, Type, function(err, analysis) {
1608
1862
  if (err) {
1609
- return cb(TypeLookup.error(err));
1863
+ return Thunk.trampoline(cb(TypeLookup.error(err)));
1610
1864
  } else {
1611
- return cb(TypeLookup.response(query, analysis));
1865
+ return Thunk.trampoline(cb(TypeLookup.response(query, analysis)));
1612
1866
  }
1613
1867
  });
1614
1868
  };
@@ -1680,10 +1934,10 @@ analyze = Gibbon.analyze = (function() {
1680
1934
  };
1681
1935
 
1682
1936
  Scope.prototype.analyze = function(push) {
1683
- var def, frame, frameScope, global, key, _i, _len, _ref4;
1684
- _ref4 = this.frame.definitions;
1685
- for (_i = 0, _len = _ref4.length; _i < _len; _i++) {
1686
- def = _ref4[_i];
1937
+ var def, frame, frameScope, global, key, _i, _len, _ref9;
1938
+ _ref9 = this.frame.definitions;
1939
+ for (_i = 0, _len = _ref9.length; _i < _len; _i++) {
1940
+ def = _ref9[_i];
1687
1941
  frameScope = this.extend(def.name, def.frame);
1688
1942
  frameScope.analyze(push);
1689
1943
  this.bindings.set(def.name, frameScope);
@@ -1751,19 +2005,14 @@ analyze = Gibbon.analyze = (function() {
1751
2005
  return _this.analyzeFlow(subFlow, global, push);
1752
2006
  },
1753
2007
  list: function(elements) {
1754
- var el, expected, rest, _i, _len, _results;
1755
- if (elements.length === 0) {
1756
- push(TypeExpr.expr(flow), TypeExpr.param('list', [TypeExpr.variable('el')]));
1757
- return;
1758
- }
1759
- expected = elements[0], rest = 2 <= elements.length ? __slice.call(elements, 1) : [];
1760
- push(TypeExpr.expr(flow), TypeExpr.param('list', [TypeExpr.expr(expected)]));
1761
- _this.analyzeFlow(expected, global, push);
2008
+ var el, elVar, _i, _len, _results;
2009
+ elVar = TypeExpr.variable('el');
2010
+ push(TypeExpr.expr(flow), TypeExpr.param('list', [elVar]));
1762
2011
  _results = [];
1763
- for (_i = 0, _len = rest.length; _i < _len; _i++) {
1764
- el = rest[_i];
2012
+ for (_i = 0, _len = elements.length; _i < _len; _i++) {
2013
+ el = elements[_i];
1765
2014
  _this.analyzeFlow(el, global, push);
1766
- _results.push(push(TypeExpr.expr(el), TypeExpr.expr(expected)));
2015
+ _results.push(push(TypeExpr.expr(el), elVar));
1767
2016
  }
1768
2017
  return _results;
1769
2018
  },
@@ -1804,16 +2053,16 @@ analyze = Gibbon.analyze = (function() {
1804
2053
  };
1805
2054
  })();
1806
2055
  solve = (function() {
1807
- var TypeError, consume, _ref4;
2056
+ var TypeError, consume, _ref9;
1808
2057
  TypeError = (function(_super) {
1809
2058
  __extends(TypeError, _super);
1810
2059
 
1811
2060
  function TypeError() {
1812
- _ref4 = TypeError.__super__.constructor.apply(this, arguments);
1813
- return _ref4;
2061
+ _ref9 = TypeError.__super__.constructor.apply(this, arguments);
2062
+ return _ref9;
1814
2063
  }
1815
2064
 
1816
- TypeError.types({
2065
+ TypeError.variants({
1817
2066
  match: ['lhs', 'rhs'],
1818
2067
  destructure: ['type'],
1819
2068
  lookup: ['query', 'id', 'error'],
@@ -1821,9 +2070,29 @@ analyze = Gibbon.analyze = (function() {
1821
2070
  func: ['node']
1822
2071
  });
1823
2072
 
2073
+ TypeError.prototype.inspect = function() {
2074
+ return this.cases({
2075
+ match: function(lhs, rhs) {
2076
+ return "could not match " + (lhs.inspect()) + " with " + (rhs.inspect());
2077
+ },
2078
+ destructure: function(type) {
2079
+ return "failure to destructure " + (type.inspect());
2080
+ },
2081
+ lookup: function(query, id, error) {
2082
+ return "error looking up " + (query.inspect()) + " on " + id + ": " + error;
2083
+ },
2084
+ circular: function(crumbs) {
2085
+ return "circular reference: " + (crumbs.join(' -> '));
2086
+ },
2087
+ func: function(node) {
2088
+ return "no such function " + node.name + " (in " + (node.inspect()) + ")";
2089
+ }
2090
+ });
2091
+ };
2092
+
1824
2093
  return TypeError;
1825
2094
 
1826
- })(Union);
2095
+ })(Variant);
1827
2096
 
1828
2097
  consume = function(array, next, body) {
1829
2098
  var loop_, push;
@@ -1831,14 +2100,16 @@ analyze = Gibbon.analyze = (function() {
1831
2100
  return array.push([x, y]);
1832
2101
  };
1833
2102
  loop_ = function() {
1834
- var lhs, rhs, _ref5;
2103
+ var lhs, rhs, _ref10;
1835
2104
  if (!(array.length > 0)) {
1836
2105
  return next();
1837
2106
  }
1838
- _ref5 = array.pop(), lhs = _ref5[0], rhs = _ref5[1];
1839
- return body(lhs, rhs, push, loop_);
2107
+ _ref10 = array.pop(), lhs = _ref10[0], rhs = _ref10[1];
2108
+ return body(lhs, rhs, push, function() {
2109
+ return new Thunk(loop_);
2110
+ });
1840
2111
  };
1841
- return loop_();
2112
+ return Thunk.trampoline(loop_());
1842
2113
  };
1843
2114
  return function(constraintMap, finish) {
1844
2115
  var errors, frameTypes, initialCrumbs, k, locks, semantics, solutions, solveEntry;
@@ -1850,13 +2121,13 @@ analyze = Gibbon.analyze = (function() {
1850
2121
  frameTypes = new Hash;
1851
2122
  locks = new Hash;
1852
2123
  solveEntry = function(crumbs, solved) {
1853
- var constraints, dependencies, done, error, frame, fullSubstitute, key, nextCrumbs, semanticAccessors, simplify, substitute, _ref5;
2124
+ var constraints, dependencies, done, error, frame, fullSubstitute, key, nextCrumbs, semanticAccessors, simplify, substitute, _ref10;
1854
2125
  key = crumbs[crumbs.length - 1];
1855
2126
  if (semantics.has(key)) {
1856
2127
  return solved();
1857
2128
  }
1858
2129
  nextCrumbs = crumbs.concat([key]);
1859
- _ref5 = constraintMap.get(key), frame = _ref5.frame, constraints = _ref5.constraints;
2130
+ _ref10 = constraintMap.get(key), frame = _ref10.frame, constraints = _ref10.constraints;
1860
2131
 
1861
2132
  locks.set(key, true);
1862
2133
  semanticAccessors = new CompMap;
@@ -1926,8 +2197,12 @@ analyze = Gibbon.analyze = (function() {
1926
2197
  return cb(error('circular', nextCrumbs));
1927
2198
  }
1928
2199
  localFrame = constraintMap.get(key).frame;
1929
- return solveEntry(nextCrumbs, function() {
1930
- return cb(frameTypes.get(key));
2200
+ return new Thunk(function() {
2201
+ return solveEntry(nextCrumbs, function() {
2202
+ return new Thunk(function() {
2203
+ return cb(frameTypes.get(key));
2204
+ });
2205
+ });
1931
2206
  });
1932
2207
  }
1933
2208
  });
@@ -1964,8 +2239,9 @@ analyze = Gibbon.analyze = (function() {
1964
2239
  return Semantic.flow(flowType(TypeExpr.expr(this)), toSemanticTree(head), tail && toSemanticTree(tail));
1965
2240
  },
1966
2241
  query: function(type, name) {
1967
- var _this = this;
1968
-
2242
+ if (!(errors.length || semanticAccessors.has(this))) {
2243
+ throw "unsolved with no errors!";
2244
+ }
1969
2245
  return semanticAccessors.get(this);
1970
2246
  },
1971
2247
  func: function(name, args) {
@@ -2106,22 +2382,22 @@ analyze = Gibbon.analyze = (function() {
2106
2382
  param: function() {
2107
2383
  return rhs.cases({
2108
2384
  param: function() {
2109
- var constraint, i, _i, _len, _ref6;
2385
+ var constraint, i, _i, _len, _ref11;
2110
2386
  if (lhs.name !== rhs.name) {
2111
2387
  return matchError();
2112
2388
  }
2113
- _ref6 = lhs.constraints;
2114
- for (i = _i = 0, _len = _ref6.length; _i < _len; i = ++_i) {
2115
- constraint = _ref6[i];
2389
+ _ref11 = lhs.constraints;
2390
+ for (i = _i = 0, _len = _ref11.length; _i < _len; i = ++_i) {
2391
+ constraint = _ref11[i];
2116
2392
  push(constraint, rhs.constraints[i]);
2117
2393
  }
2118
2394
  return next();
2119
2395
  },
2120
2396
  query: function() {
2121
- var c, i, _i, _len, _ref6;
2122
- _ref6 = lhs.constraints;
2123
- for (i = _i = 0, _len = _ref6.length; _i < _len; i = ++_i) {
2124
- c = _ref6[i];
2397
+ var c, i, _i, _len, _ref11;
2398
+ _ref11 = lhs.constraints;
2399
+ for (i = _i = 0, _len = _ref11.length; _i < _len; i = ++_i) {
2400
+ c = _ref11[i];
2125
2401
  push(c, TypeExpr.destructure(rhs, lhs.type(i)));
2126
2402
  }
2127
2403
  return next();
@@ -2136,11 +2412,11 @@ analyze = Gibbon.analyze = (function() {
2136
2412
  });
2137
2413
  };
2138
2414
  initialCrumbs = (function() {
2139
- var _i, _len, _ref5, _results;
2140
- _ref5 = constraintMap.keys();
2415
+ var _i, _len, _ref10, _results;
2416
+ _ref10 = constraintMap.keys();
2141
2417
  _results = [];
2142
- for (_i = 0, _len = _ref5.length; _i < _len; _i++) {
2143
- k = _ref5[_i];
2418
+ for (_i = 0, _len = _ref10.length; _i < _len; _i++) {
2419
+ k = _ref10[_i];
2144
2420
  _results.push([k]);
2145
2421
  }
2146
2422
  return _results;
@@ -2163,20 +2439,16 @@ analyze = Gibbon.analyze = (function() {
2163
2439
  return solve(constraints, cb);
2164
2440
  };
2165
2441
  })();
2166
- // Generated by CoffeeScript 1.6.3
2167
- var CachingPromise, Dependency, Failure, Promise, Thunk, UnitPromise, Value, eval_, _ref, _ref1, _ref2, _ref3,
2168
- __hasProp = {}.hasOwnProperty,
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; };
2170
2442
 
2171
2443
  Value = Gibbon.Value = Value = (function(_super) {
2172
2444
  __extends(Value, _super);
2173
2445
 
2174
2446
  function Value() {
2175
- _ref = Value.__super__.constructor.apply(this, arguments);
2176
- return _ref;
2447
+ _ref9 = Value.__super__.constructor.apply(this, arguments);
2448
+ return _ref9;
2177
2449
  }
2178
2450
 
2179
- Value.types({
2451
+ Value.variants({
2180
2452
  string: ['value'],
2181
2453
  number: ['value'],
2182
2454
  boolean: ['value'],
@@ -2217,6 +2489,121 @@ Value = Gibbon.Value = Value = (function(_super) {
2217
2489
  throw new Error('invalid value: ' + o);
2218
2490
  };
2219
2491
 
2492
+ Value.interpret = function(o, type) {
2493
+ var _this = this;
2494
+ return type.cases({
2495
+ pair: function(first, second) {
2496
+ if (!('first' in o && 'second' in o)) {
2497
+ throw 'bad value';
2498
+ }
2499
+ return Value.pair(_this.interpret(o.first, first), _this.interpret(o.second, second));
2500
+ },
2501
+ list: function(listOf) {
2502
+ var e;
2503
+ if (!isArray(o)) {
2504
+ throw 'bad value';
2505
+ }
2506
+ return Value.list((function() {
2507
+ var _i, _len, _results;
2508
+ _results = [];
2509
+ for (_i = 0, _len = o.length; _i < _len; _i++) {
2510
+ e = o[_i];
2511
+ _results.push(this.interpret(e, listOf));
2512
+ }
2513
+ return _results;
2514
+ }).call(_this));
2515
+ },
2516
+ entity: function(type) {
2517
+ if (typeof o !== 'number') {
2518
+ throw 'bad value';
2519
+ }
2520
+ return Value.entity(type, o);
2521
+ },
2522
+ numeric: function() {
2523
+ if (typeof o !== 'number') {
2524
+ throw 'bad value';
2525
+ }
2526
+ return Value.number(o);
2527
+ },
2528
+ string: function() {
2529
+ if (typeof o !== 'string') {
2530
+ throw 'bad value';
2531
+ }
2532
+ return Value.string(o);
2533
+ },
2534
+ bool: function() {
2535
+ if (typeof o !== 'boolean') {
2536
+ throw 'bad value';
2537
+ }
2538
+ return Value.boolean(o);
2539
+ },
2540
+ other: function() {
2541
+ throw "could not return object of type " + (type.inspect());
2542
+ }
2543
+ });
2544
+ };
2545
+
2546
+ Value.prototype.asPrimitive = function() {
2547
+ return this.cases({
2548
+ string: function(v) {
2549
+ return v;
2550
+ },
2551
+ number: function(v) {
2552
+ return v;
2553
+ },
2554
+ boolean: function(v) {
2555
+ return v;
2556
+ },
2557
+ block: function(v) {
2558
+ return v;
2559
+ },
2560
+ list: function(els) {
2561
+ var e, _i, _len, _results;
2562
+ _results = [];
2563
+ for (_i = 0, _len = els.length; _i < _len; _i++) {
2564
+ e = els[_i];
2565
+ _results.push(e.asPrimitive());
2566
+ }
2567
+ return _results;
2568
+ },
2569
+ pair: function(first, second) {
2570
+ return {
2571
+ first: first.asPrimitive(),
2572
+ second: second.asPrimitive()
2573
+ };
2574
+ },
2575
+ entity: function(_, id) {
2576
+ return id;
2577
+ }
2578
+ });
2579
+ };
2580
+
2581
+ Value.prototype.inspect = function() {
2582
+ return this.cases({
2583
+ list: function(els) {
2584
+ var e;
2585
+ return "(list " + (((function() {
2586
+ var _i, _len, _results;
2587
+ _results = [];
2588
+ for (_i = 0, _len = els.length; _i < _len; _i++) {
2589
+ e = els[_i];
2590
+ _results.push(e.inspect());
2591
+ }
2592
+ return _results;
2593
+ })()).join(' ')) + ")";
2594
+ },
2595
+ pair: function(first, second) {
2596
+ return "(pair " + (first.inspect()) + " " + (second.inspect()) + ")";
2597
+ },
2598
+ entity: function(id) {
2599
+ return "(entity " + id + ")";
2600
+ },
2601
+ other: function() {
2602
+ return '' + this.asPrimitive();
2603
+ }
2604
+ });
2605
+ };
2606
+
2220
2607
  Value.prototype.promise = function() {
2221
2608
  return Promise.unit(this.map(function(x) {
2222
2609
  return x.promise();
@@ -2316,23 +2703,7 @@ Value = Gibbon.Value = Value = (function(_super) {
2316
2703
 
2317
2704
  return Value;
2318
2705
 
2319
- })(Union);
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
- })();
2706
+ })(Variant);
2336
2707
 
2337
2708
  Promise = (function() {
2338
2709
  function Promise(forceRaw) {
@@ -2511,8 +2882,8 @@ UnitPromise = (function(_super) {
2511
2882
  __extends(UnitPromise, _super);
2512
2883
 
2513
2884
  function UnitPromise() {
2514
- _ref1 = UnitPromise.__super__.constructor.apply(this, arguments);
2515
- return _ref1;
2885
+ _ref10 = UnitPromise.__super__.constructor.apply(this, arguments);
2886
+ return _ref10;
2516
2887
  }
2517
2888
 
2518
2889
  UnitPromise.prototype.cache = function() {
@@ -2562,11 +2933,11 @@ Gibbon.Failure = Failure = (function(_super) {
2562
2933
  __extends(Failure, _super);
2563
2934
 
2564
2935
  function Failure() {
2565
- _ref2 = Failure.__super__.constructor.apply(this, arguments);
2566
- return _ref2;
2936
+ _ref11 = Failure.__super__.constructor.apply(this, arguments);
2937
+ return _ref11;
2567
2938
  }
2568
2939
 
2569
- Failure.types({
2940
+ Failure.variants({
2570
2941
  query: ['id', 'annotations'],
2571
2942
  composite: ['failures'],
2572
2943
  message: ['message']
@@ -2603,17 +2974,17 @@ Gibbon.Failure = Failure = (function(_super) {
2603
2974
 
2604
2975
  return Failure;
2605
2976
 
2606
- })(Union);
2977
+ })(Variant);
2607
2978
 
2608
2979
  Gibbon.Dependency = Dependency = (function(_super) {
2609
2980
  __extends(Dependency, _super);
2610
2981
 
2611
2982
  function Dependency() {
2612
- _ref3 = Dependency.__super__.constructor.apply(this, arguments);
2613
- return _ref3;
2983
+ _ref12 = Dependency.__super__.constructor.apply(this, arguments);
2984
+ return _ref12;
2614
2985
  }
2615
2986
 
2616
- Dependency.types({
2987
+ Dependency.variants({
2617
2988
  query: ['entity', 'query'],
2618
2989
  lexical: ['key'],
2619
2990
  failure: ['failure']
@@ -2628,7 +2999,7 @@ Gibbon.Dependency = Dependency = (function(_super) {
2628
2999
  if (entity.id !== other.entity.id) {
2629
3000
  return false;
2630
3001
  }
2631
- if (query.annotations !== other.query.annotations) {
3002
+ if (JSON.stringify(query) !== JSON.stringify(other.query)) {
2632
3003
  return false;
2633
3004
  }
2634
3005
  return true;
@@ -2644,8 +3015,8 @@ Gibbon.Dependency = Dependency = (function(_super) {
2644
3015
 
2645
3016
  Dependency.prototype.inspect = function() {
2646
3017
  return this.cases({
2647
- lookup: function(entity, query) {
2648
- return "" + (query.inspect()) + "<" + entity.id + ">";
3018
+ query: function(entity, query) {
3019
+ return "" + (JSON.stringify(query)) + "<" + entity + ">";
2649
3020
  },
2650
3021
  lexical: function(definition) {
2651
3022
  return "@" + definition.name;
@@ -2658,7 +3029,7 @@ Gibbon.Dependency = Dependency = (function(_super) {
2658
3029
 
2659
3030
  return Dependency;
2660
3031
 
2661
- })(Union);
3032
+ })(Variant);
2662
3033
 
2663
3034
  eval_ = Gibbon["eval"] = (function() {
2664
3035
  var Context, Scope, combine, unit, vFalse, vTrue;
@@ -2677,10 +3048,10 @@ eval_ = Gibbon["eval"] = (function() {
2677
3048
  Context.prototype.evalAll = function() {
2678
3049
  var results, toForce,
2679
3050
  _this = this;
2680
- this.definitions.each(function(key, ir) {
3051
+ this.definitions.each(function(key, expr) {
2681
3052
  var lazy;
2682
3053
  lazy = Promise.lazy(function() {
2683
- return _this.rootScope["eval"](ir).then(function(x) {
3054
+ return _this.rootScope["eval"](expr).then(function(x) {
2684
3055
  return x.resolve();
2685
3056
  });
2686
3057
  });
@@ -2700,8 +3071,8 @@ eval_ = Gibbon["eval"] = (function() {
2700
3071
  }));
2701
3072
  });
2702
3073
  return combine(toForce).then(function() {
2703
- var deps, val, _ref4;
2704
- _ref4 = results.get('/'), val = _ref4[0], deps = _ref4[1];
3074
+ var deps, val, _ref13;
3075
+ _ref13 = results.get('/'), val = _ref13[0], deps = _ref13[1];
2705
3076
  if (val) {
2706
3077
  return Promise.unit(results);
2707
3078
  } else {
@@ -2761,10 +3132,10 @@ eval_ = Gibbon["eval"] = (function() {
2761
3132
  };
2762
3133
 
2763
3134
  Scope.prototype["let"] = function(bindings) {
2764
- var ext, k, v, _i, _len, _ref4;
3135
+ var ext, k, v, _i, _len, _ref13;
2765
3136
  ext = new Hash;
2766
3137
  for (_i = 0, _len = bindings.length; _i < _len; _i++) {
2767
- _ref4 = bindings[_i], k = _ref4[0], v = _ref4[1];
3138
+ _ref13 = bindings[_i], k = _ref13[0], v = _ref13[1];
2768
3139
  ext.set(k, v.cache());
2769
3140
  }
2770
3141
  return this.extend(ext);
@@ -2820,9 +3191,9 @@ eval_ = Gibbon["eval"] = (function() {
2820
3191
  return this.get(varName);
2821
3192
  };
2822
3193
 
2823
- Scope.prototype["eval"] = function(ir) {
3194
+ Scope.prototype["eval"] = function(expr) {
2824
3195
  var _this = this;
2825
- return ir.cases({
3196
+ return expr.cases({
2826
3197
  global: function() {
2827
3198
  return _this.get('@');
2828
3199
  },
@@ -2842,16 +3213,14 @@ eval_ = Gibbon["eval"] = (function() {
2842
3213
  });
2843
3214
  },
2844
3215
  bind: function(name, val, expr) {
2845
- return _this["eval"](val).then(function(v) {
2846
- return _this["let"]([[name, v]])["eval"](expr);
2847
- });
3216
+ return _this["let"]([[name, _this["eval"](val)]])["eval"](expr);
2848
3217
  },
2849
3218
  delist: function(expr, index) {
2850
3219
  var evaled;
2851
3220
  evaled = [_this["eval"](expr), _this["eval"](index)];
2852
3221
  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);
3222
+ var elements, value, _ref13, _ref14;
3223
+ (_ref13 = _arg[0], elements = _ref13.elements), (_ref14 = _arg[1], value = _ref14.value);
2855
3224
  return elements[value];
2856
3225
  });
2857
3226
  },
@@ -2880,7 +3249,13 @@ eval_ = Gibbon["eval"] = (function() {
2880
3249
  if (i >= elements.length) {
2881
3250
  return _this["eval"](out);
2882
3251
  }
2883
- return _this["let"]([[idxArg, unit(Value.number(i))], [arg, elements[i]], [accumArg, _loop(i + 1)]])["eval"](body);
3252
+ return _this["let"]([
3253
+ [idxArg, unit(Value.number(i))], [arg, elements[i]], [
3254
+ accumArg, Promise.lazy(function() {
3255
+ return _loop(i + 1);
3256
+ })
3257
+ ]
3258
+ ])["eval"](body);
2884
3259
  };
2885
3260
  return _loop(0);
2886
3261
  });
@@ -2908,11 +3283,11 @@ eval_ = Gibbon["eval"] = (function() {
2908
3283
  var el, first, i, pairs, second;
2909
3284
  first = _arg[0], second = _arg[1];
2910
3285
  pairs = (function() {
2911
- var _i, _len, _ref4, _results;
2912
- _ref4 = first.elements;
3286
+ var _i, _len, _ref13, _results;
3287
+ _ref13 = first.elements;
2913
3288
  _results = [];
2914
- for (i = _i = 0, _len = _ref4.length; _i < _len; i = ++_i) {
2915
- el = _ref4[i];
3289
+ for (i = _i = 0, _len = _ref13.length; _i < _len; i = ++_i) {
3290
+ el = _ref13[i];
2916
3291
  _results.push(Value.pair(el, second.elements[i]));
2917
3292
  }
2918
3293
  return _results;
@@ -2959,14 +3334,25 @@ eval_ = Gibbon["eval"] = (function() {
2959
3334
  promise = elements[_i];
2960
3335
  _results.push(promise.then(function(e) {
2961
3336
  return e.resolve().map(function(e) {
2962
- return [e.promise()];
3337
+ return e.promise();
2963
3338
  });
2964
- }).or(unit([])));
3339
+ }).or(unit(null)));
2965
3340
  }
2966
3341
  return _results;
2967
3342
  }).call(_this);
2968
- return combine(wrapped).map(function(lists) {
2969
- return Value.list(catLists(lists));
3343
+ return combine(wrapped).map(function(withNulls) {
3344
+ var e;
3345
+ return Value.list((function() {
3346
+ var _i, _len, _results;
3347
+ _results = [];
3348
+ for (_i = 0, _len = withNulls.length; _i < _len; _i++) {
3349
+ e = withNulls[_i];
3350
+ if (e) {
3351
+ _results.push(e);
3352
+ }
3353
+ }
3354
+ return _results;
3355
+ })());
2970
3356
  });
2971
3357
  });
2972
3358
  },
@@ -3002,18 +3388,18 @@ eval_ = Gibbon["eval"] = (function() {
3002
3388
  fail: function(message) {
3003
3389
  return Promise.fail(Failure.message(message));
3004
3390
  },
3005
- binop: function(op, lhs, rhs) {
3391
+ op1: function(name, value) {
3392
+ return _this["eval"](value).map(function(v) {
3393
+ return EXTERN[name](v.value);
3394
+ });
3395
+ },
3396
+ op2: function(op, lhs, rhs) {
3006
3397
  return combine([_this["eval"](lhs), _this["eval"](rhs)]).map(function(_arg) {
3007
3398
  var l, r;
3008
3399
  l = _arg[0], r = _arg[1];
3009
3400
  return OPERATORS[op](l.value, r.value);
3010
3401
  });
3011
3402
  },
3012
- extern: function(name, value) {
3013
- return _this["eval"](value).map(function(v) {
3014
- return EXTERN[name](v.value);
3015
- });
3016
- },
3017
3403
  rescue: function(expr, default_) {
3018
3404
  return _this["eval"](expr).or(_this["eval"](default_));
3019
3405
  }
@@ -3024,46 +3410,25 @@ eval_ = Gibbon["eval"] = (function() {
3024
3410
 
3025
3411
  })();
3026
3412
  return function(semantics, table, id, client, finish) {
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
- });
3035
- entity = Value.entity(table, id);
3036
- onFailure = function(fail) {
3037
- return finish(fail);
3038
- };
3039
- onSuccess = function(vals) {
3040
-
3041
- return finish(null, vals);
3042
- };
3043
- return new Context(client, entity, allCompiled).evalAll().force(onFailure, onSuccess);
3413
+ return Gibbon.compile(semantics).run('/', id, client, finish);
3044
3414
  };
3045
3415
  })();
3046
- // Generated by CoffeeScript 1.6.3
3047
- var IR, _ref,
3048
- __hasProp = {}.hasOwnProperty,
3049
- __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; };
3050
3416
 
3051
- Gibbon.IR = IR = (function(_super) {
3417
+ Gibbon.Core = Core = (function(_super) {
3052
3418
  var nameGen;
3053
3419
 
3054
- __extends(IR, _super);
3420
+ __extends(Core, _super);
3055
3421
 
3056
- function IR() {
3057
- _ref = IR.__super__.constructor.apply(this, arguments);
3058
- return _ref;
3422
+ function Core() {
3423
+ _ref13 = Core.__super__.constructor.apply(this, arguments);
3424
+ return _ref13;
3059
3425
  }
3060
3426
 
3061
- IR.types({
3427
+ Core.variants({
3062
3428
  global: [],
3063
3429
  constant: ['value'],
3064
3430
  variable: ['name'],
3065
3431
  branch: ['cond', 'ifTrue', 'ifFalse'],
3066
- bind: ['name', 'value', 'expr'],
3067
3432
  delist: ['expr', 'index'],
3068
3433
  depair: ['expr', 'key'],
3069
3434
  list: ['elements'],
@@ -3079,52 +3444,58 @@ Gibbon.IR = IR = (function(_super) {
3079
3444
  query: ['expr', 'annotations'],
3080
3445
  localQuery: ['key'],
3081
3446
  fail: ['message'],
3082
- binop: ['op', 'lhs', 'rhs'],
3083
- extern: ['name', 'value'],
3084
- rescue: ['expr', 'default']
3447
+ op1: ['op', 'arg'],
3448
+ op2: ['op', 'lhs', 'rhs'],
3449
+ rescue: ['expr', 'default'],
3450
+ next: ['cont', 'args'],
3451
+ bind: ['name', 'value', 'expr']
3085
3452
  });
3086
3453
 
3087
- IR.makeVariable = function(name) {
3088
- return IR.variable(nameGen(name));
3454
+ Core.makeVariable = function(name) {
3455
+ return Core.variable(nameGen(name));
3089
3456
  };
3090
3457
 
3091
- IR.prototype.branch = function(ifTrue, ifFalse) {
3092
- return IR.branch(this, ifTrue, ifFalse);
3458
+ Core.prototype.branch = function(ifTrue, ifFalse) {
3459
+ return Core.branch(this, ifTrue, ifFalse);
3093
3460
  };
3094
3461
 
3095
- IR.prototype.delist = function(index) {
3096
- return IR.delist(this, index);
3462
+ Core.prototype.delist = function(index) {
3463
+ return Core.delist(this, index);
3097
3464
  };
3098
3465
 
3099
- IR.prototype.depair = function(key) {
3100
- return IR.depair(this, key);
3466
+ Core.prototype.depair = function(key) {
3467
+ return Core.depair(this, key);
3101
3468
  };
3102
3469
 
3103
- IR.prototype.query = function(annotations) {
3104
- return IR.query(this, annotations);
3470
+ Core.prototype.query = function(annotations) {
3471
+ return Core.query(this, annotations);
3105
3472
  };
3106
3473
 
3107
- IR.prototype.binop = function(op, other) {
3108
- return IR.binop(op, this, other);
3474
+ Core.prototype.op1 = function(op) {
3475
+ return Core.op1(op, this);
3109
3476
  };
3110
3477
 
3111
- IR.prototype.extern = function(name) {
3112
- return IR.extern(name, this);
3478
+ Core.prototype.op2 = function(op, other) {
3479
+ return Core.op2(op, this, other);
3113
3480
  };
3114
3481
 
3115
- IR.prototype.app = function(arg) {
3116
- return IR.app(this, arg);
3482
+ Core.prototype.app = function(arg) {
3483
+ return Core.app(this, arg);
3117
3484
  };
3118
3485
 
3119
- IR.prototype.squish = function() {
3120
- return IR.squishList(this);
3486
+ Core.prototype.squish = function() {
3487
+ return Core.squishList(this);
3121
3488
  };
3122
3489
 
3123
- IR.prototype.len = function() {
3124
- return IR.len(this);
3490
+ Core.prototype.len = function() {
3491
+ return Core.len(this);
3125
3492
  };
3126
3493
 
3127
- IR.prototype.isAsync = function() {
3494
+ Core.prototype.rescue = function(alt) {
3495
+ return Core.rescue(this, alt);
3496
+ };
3497
+
3498
+ Core.prototype.isAsync = function() {
3128
3499
  return this.cases({
3129
3500
  query: function() {
3130
3501
  return true;
@@ -3133,10 +3504,10 @@ Gibbon.IR = IR = (function(_super) {
3133
3504
  return true;
3134
3505
  },
3135
3506
  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];
3507
+ var sub, _i, _len, _ref14;
3508
+ _ref14 = this.subtrees();
3509
+ for (_i = 0, _len = _ref14.length; _i < _len; _i++) {
3510
+ sub = _ref14[_i];
3140
3511
  if (sub.isAsync()) {
3141
3512
  return true;
3142
3513
  }
@@ -3146,7 +3517,7 @@ Gibbon.IR = IR = (function(_super) {
3146
3517
  });
3147
3518
  };
3148
3519
 
3149
- IR.prototype.isSimple = function() {
3520
+ Core.prototype.isSimple = function() {
3150
3521
  return this.cases({
3151
3522
  variable: function() {
3152
3523
  return true;
@@ -3157,50 +3528,125 @@ Gibbon.IR = IR = (function(_super) {
3157
3528
  global: function() {
3158
3529
  return true;
3159
3530
  },
3160
- binop: function(op, l, r) {
3161
- return l.isSimple() && r.isSimple();
3531
+ other: function() {
3532
+ return false;
3533
+ }
3534
+ });
3535
+ };
3536
+
3537
+ Core.prototype.alwaysSucceeds = function() {
3538
+ return this.cases({
3539
+ query: function() {
3540
+ return false;
3541
+ },
3542
+ localQuery: function() {
3543
+ return false;
3544
+ },
3545
+ fail: function() {
3546
+ return false;
3547
+ },
3548
+ rescue: function(e, default_) {
3549
+ return e.alwaysSucceeds() || default_.alwaysSucceeds();
3550
+ },
3551
+ squishList: function() {
3552
+ return true;
3553
+ },
3554
+ other: function() {
3555
+ var subtree, _i, _len, _ref14;
3556
+ _ref14 = this.subtrees();
3557
+ for (_i = 0, _len = _ref14.length; _i < _len; _i++) {
3558
+ subtree = _ref14[_i];
3559
+ console.log("checking alwaysSucceeds " + (subtree.inspect()));
3560
+ if (!subtree.alwaysSucceeds()) {
3561
+ return false;
3562
+ }
3563
+ }
3564
+ return true;
3565
+ }
3566
+ });
3567
+ };
3568
+
3569
+ Core.prototype.alwaysFails = function() {
3570
+ return this.cases({
3571
+ query: function() {
3572
+ return false;
3573
+ },
3574
+ localQuery: function() {
3575
+ return false;
3576
+ },
3577
+ fail: function() {
3578
+ return true;
3579
+ },
3580
+ rescue: function(e, default_) {
3581
+ return e.alwaysFails() && default_.alwaysFails();
3162
3582
  },
3163
- extern: function(n, v) {
3164
- return v.isSimple();
3583
+ squishList: function() {
3584
+ return false;
3165
3585
  },
3166
3586
  other: function() {
3587
+ var subtree, _i, _len, _ref14;
3588
+ _ref14 = this.subtrees();
3589
+ for (_i = 0, _len = _ref14.length; _i < _len; _i++) {
3590
+ subtree = _ref14[_i];
3591
+ console.log("checking alwaysFails " + (subtree.inspect()));
3592
+ if (subtree.alwaysFails()) {
3593
+ return true;
3594
+ }
3595
+ }
3167
3596
  return false;
3168
3597
  }
3169
3598
  });
3170
3599
  };
3171
3600
 
3172
- IR.prototype.seq = function(name, f) {
3601
+ Core.prototype.seq = function(name, f) {
3173
3602
  name = nameGen(name);
3174
- return IR.bind(name, this, f(IR.variable(name)));
3603
+ return Core.bind(name, this, f(Core.variable(name)));
3175
3604
  };
3176
3605
 
3177
- IR.prototype.mapList = function(f) {
3606
+ Core.prototype.mapList = function(f) {
3178
3607
  var elName, ixName;
3179
3608
  elName = nameGen('el');
3180
3609
  ixName = nameGen('i');
3181
- return IR.mapList(this, elName, ixName, f(IR.variable(elName), IR.variable(ixName)));
3610
+ return Core.mapList(this, elName, ixName, f(Core.variable(elName), Core.variable(ixName)));
3182
3611
  };
3183
3612
 
3184
- IR.prototype.foldList = function(init, f) {
3613
+ Core.prototype.foldList = function(init, f) {
3185
3614
  var accumName, body, elName, ixName;
3186
3615
  elName = nameGen('el');
3187
3616
  ixName = nameGen('i');
3188
3617
  accumName = nameGen('next');
3189
- body = f(IR.variable(elName), IR.variable(accumName), IR.variable(ixName));
3190
- return IR.foldList(this, init, elName, accumName, ixName, body);
3618
+ body = f(Core.variable(elName), Core.variable(accumName), Core.variable(ixName));
3619
+ return Core.foldList(this, init, elName, accumName, ixName, body);
3191
3620
  };
3192
3621
 
3193
- IR.prototype.zipList = function(other) {
3194
- return IR.zipLists(this, other);
3622
+ Core.prototype.zipList = function(other) {
3623
+ return Core.zipLists(this, other);
3195
3624
  };
3196
3625
 
3197
- IR.prototype.filterList = function(f) {
3626
+ Core.prototype.filterList = function(f) {
3198
3627
  var name;
3199
3628
  name = nameGen('el');
3200
- return IR.filterList(this, name, f(IR.variable(name)));
3629
+ return Core.filterList(this, name, f(Core.variable(name)));
3201
3630
  };
3202
3631
 
3203
- IR.prototype.subtrees = function() {
3632
+ Core.prototype.variables = function() {
3633
+ var s;
3634
+ if (this._tag === 'variable') {
3635
+ return [this];
3636
+ }
3637
+ return catLists((function() {
3638
+ var _i, _len, _ref14, _results;
3639
+ _ref14 = this.subtrees();
3640
+ _results = [];
3641
+ for (_i = 0, _len = _ref14.length; _i < _len; _i++) {
3642
+ s = _ref14[_i];
3643
+ _results.push(s.variables());
3644
+ }
3645
+ return _results;
3646
+ }).call(this));
3647
+ };
3648
+
3649
+ Core.prototype.subtrees = function() {
3204
3650
  var double, single;
3205
3651
  single = function(x) {
3206
3652
  return [x];
@@ -3234,12 +3680,12 @@ Gibbon.IR = IR = (function(_super) {
3234
3680
  },
3235
3681
  app: double,
3236
3682
  query: single,
3237
- binop: function(op, l, r) {
3238
- return [l, r];
3239
- },
3240
- extern: function(n, v) {
3683
+ op1: function(op, v) {
3241
3684
  return [v];
3242
3685
  },
3686
+ op2: function(op, l, r) {
3687
+ return [l, r];
3688
+ },
3243
3689
  rescue: double,
3244
3690
  other: function() {
3245
3691
  return [];
@@ -3247,23 +3693,23 @@ Gibbon.IR = IR = (function(_super) {
3247
3693
  });
3248
3694
  };
3249
3695
 
3250
- IR.prototype.map = function(f) {
3696
+ Core.prototype.map = function(f) {
3251
3697
  return this.cases({
3252
3698
  branch: function(c, tr, fa) {
3253
- return IR.branch(f(c), f(tr), f(fa));
3699
+ return Core.branch(f(c), f(tr), f(fa));
3254
3700
  },
3255
3701
  bind: function(n, v, e) {
3256
- return IR.bind(n, f(v), f(e));
3702
+ return Core.bind(n, f(v), f(e));
3257
3703
  },
3258
3704
  delist: function(e, i) {
3259
- return IR.delist(f(e), i);
3705
+ return Core.delist(f(e), i);
3260
3706
  },
3261
3707
  depair: function(e, k) {
3262
- return IR.depair(f(e), k);
3708
+ return Core.depair(f(e), k);
3263
3709
  },
3264
3710
  list: function(els) {
3265
3711
  var e;
3266
- return IR.list((function() {
3712
+ return Core.list((function() {
3267
3713
  var _i, _len, _results;
3268
3714
  _results = [];
3269
3715
  for (_i = 0, _len = els.length; _i < _len; _i++) {
@@ -3274,34 +3720,40 @@ Gibbon.IR = IR = (function(_super) {
3274
3720
  })());
3275
3721
  },
3276
3722
  foldList: function(l, o, a, i, aa, b) {
3277
- return IR.foldList(f(l), f(o), a, i, aa, f(b));
3723
+ return Core.foldList(f(l), f(o), a, i, aa, f(b));
3278
3724
  },
3279
3725
  mapList: function(l, a, i, b) {
3280
- return IR.mapList(f(l), i, a, f(b));
3726
+ return Core.mapList(f(l), i, a, f(b));
3281
3727
  },
3282
3728
  zipLists: function(l, r) {
3283
- return IR.zipLists(f(l), f(r));
3729
+ return Core.zipLists(f(l), f(r));
3730
+ },
3731
+ filterList: function(l, a, b) {
3732
+ return Core.filterList(f(l), a, f(b));
3284
3733
  },
3285
3734
  len: function(l) {
3286
- return IR.len(f(l));
3735
+ return Core.len(f(l));
3287
3736
  },
3288
3737
  pair: function(x, y) {
3289
- return IR.pair(f(x), f(y));
3738
+ return Core.pair(f(x), f(y));
3290
3739
  },
3291
3740
  block: function(n, b) {
3292
- return IR.block(n, f(b));
3741
+ return Core.block(n, f(b));
3293
3742
  },
3294
3743
  app: function(b, a) {
3295
- return IR.app(f(b), f(a));
3744
+ return Core.app(f(b), f(a));
3296
3745
  },
3297
3746
  query: function(e, a) {
3298
- return IR.query(f(e), a);
3747
+ return Core.query(f(e), a);
3299
3748
  },
3300
- binop: function(o, l, r) {
3301
- return IR.binop(o, f(l), f(r));
3749
+ op1: function(o, v) {
3750
+ return Core.op1(o, f(v));
3302
3751
  },
3303
- extern: function(n, v) {
3304
- return IR.extern(n, f(v));
3752
+ op2: function(o, l, r) {
3753
+ return Core.op2(o, f(l), f(r));
3754
+ },
3755
+ squishList: function(l) {
3756
+ return Core.squishList(f(l));
3305
3757
  },
3306
3758
  other: function() {
3307
3759
  return this;
@@ -3309,7 +3761,7 @@ Gibbon.IR = IR = (function(_super) {
3309
3761
  });
3310
3762
  };
3311
3763
 
3312
- IR.prototype.replace = function(expr, other) {
3764
+ Core.prototype.replace = function(expr, other) {
3313
3765
  if (this.equals(expr)) {
3314
3766
  return other;
3315
3767
  }
@@ -3318,7 +3770,7 @@ Gibbon.IR = IR = (function(_super) {
3318
3770
  });
3319
3771
  };
3320
3772
 
3321
- IR.prototype.subst = function(varName, expr) {
3773
+ Core.prototype.subst = function(varName, expr) {
3322
3774
  if (this._tag === 'variable' && this.name === varName) {
3323
3775
  return expr;
3324
3776
  }
@@ -3327,7 +3779,58 @@ Gibbon.IR = IR = (function(_super) {
3327
3779
  });
3328
3780
  };
3329
3781
 
3330
- IR.prototype.equals = function(other) {
3782
+ Core.prototype.isStrictIn = function(needle) {
3783
+ if (this.equals(needle)) {
3784
+ return true;
3785
+ }
3786
+ return this.cases({
3787
+ branch: function(cond, ifTrue, ifFalse) {
3788
+ return cond.isStrictIn(needle) || (ifTrue.isStrictIn(needle) && ifFalse.isStrictIn(needle));
3789
+ },
3790
+ other: function() {
3791
+ var subtree, _i, _len, _ref14;
3792
+ _ref14 = this.subtrees();
3793
+ for (_i = 0, _len = _ref14.length; _i < _len; _i++) {
3794
+ subtree = _ref14[_i];
3795
+ if (subtree.isStrictIn(needle)) {
3796
+ return true;
3797
+ }
3798
+ }
3799
+ return false;
3800
+ }
3801
+ });
3802
+ };
3803
+
3804
+ Core.prototype.contains = function(needle) {
3805
+ var subtree, _i, _len, _ref14;
3806
+ if (this.equals(needle)) {
3807
+ return true;
3808
+ }
3809
+ _ref14 = this.subtrees();
3810
+ for (_i = 0, _len = _ref14.length; _i < _len; _i++) {
3811
+ subtree = _ref14[_i];
3812
+ if (subtree.contains(needle)) {
3813
+ return true;
3814
+ }
3815
+ }
3816
+ return false;
3817
+ };
3818
+
3819
+ Core.prototype.containsInNonTailPosition = function(needle) {
3820
+ if (this.equals(needle)) {
3821
+ return false;
3822
+ }
3823
+ return this.cases({
3824
+ branch: function(cond, ifTrue, ifFalse) {
3825
+ return cond.contains(needle) || ifTrue.containsInNonTailPosition(needle) || ifFalse.containsInNonTailPosition(needle);
3826
+ },
3827
+ other: function() {
3828
+ return this.contains(needle);
3829
+ }
3830
+ });
3831
+ };
3832
+
3833
+ Core.prototype.equals = function(other) {
3331
3834
  if (this._tag !== other._tag) {
3332
3835
  return false;
3333
3836
  }
@@ -3349,7 +3852,7 @@ Gibbon.IR = IR = (function(_super) {
3349
3852
  if (!val.equals(other.value)) {
3350
3853
  return false;
3351
3854
  }
3352
- subst = other.expr.subst(other.name, IR.variable(name));
3855
+ subst = other.expr.subst(other.name, Core.variable(name));
3353
3856
  return expr.equals(subst);
3354
3857
  },
3355
3858
  delist: function(expr, index) {
@@ -3376,7 +3879,7 @@ Gibbon.IR = IR = (function(_super) {
3376
3879
  if (!out.equals(other.out)) {
3377
3880
  return false;
3378
3881
  }
3379
- subst = other.body.subst(other.arg, IR.variable(arg)).subst(other.accumArg, IR.variable(accumArg)).subst(other.idxArg, IR.variable(idxArg));
3882
+ subst = other.body.subst(other.arg, Core.variable(arg)).subst(other.accumArg, Core.variable(accumArg)).subst(other.idxArg, Core.variable(idxArg));
3380
3883
  return body.equals(subst);
3381
3884
  },
3382
3885
  mapList: function(list, arg, idxArg, body) {
@@ -3384,7 +3887,7 @@ Gibbon.IR = IR = (function(_super) {
3384
3887
  if (!list.equals(other.list)) {
3385
3888
  return false;
3386
3889
  }
3387
- subst = other.body.subst(other.arg, IR.variable(arg)).subst(other.idxArg, IR.variable(idxArg));
3890
+ subst = other.body.subst(other.arg, Core.variable(arg)).subst(other.idxArg, Core.variable(idxArg));
3388
3891
  return body.equals(subst);
3389
3892
  },
3390
3893
  zipLists: function(first, second) {
@@ -3394,7 +3897,7 @@ Gibbon.IR = IR = (function(_super) {
3394
3897
  if (!list.equals(other.list)) {
3395
3898
  return false;
3396
3899
  }
3397
- return body.equals(other.body.subst(other.arg, IR.variable(arg)));
3900
+ return body.equals(other.body.subst(other.arg, Core.variable(arg)));
3398
3901
  },
3399
3902
  squishList: function(list) {
3400
3903
  return list.equals(other.list);
@@ -3420,11 +3923,11 @@ Gibbon.IR = IR = (function(_super) {
3420
3923
  fail: function() {
3421
3924
  return false;
3422
3925
  },
3423
- binop: function(op, lhs, rhs) {
3926
+ op2: function(op, lhs, rhs) {
3424
3927
  return op === other.op && lhs.equals(other.lhs) && rhs.equals(other.rhs);
3425
3928
  },
3426
- extern: function(name, value) {
3427
- return name === other.name && value.equals(other.value);
3929
+ op1: function(op, arg) {
3930
+ return op === other.op && arg.equals(other.arg);
3428
3931
  },
3429
3932
  rescue: function(expr, default_) {
3430
3933
  return expr.equals(other.expr) && default_.equals(other["default"]);
@@ -3440,7 +3943,7 @@ Gibbon.IR = IR = (function(_super) {
3440
3943
  };
3441
3944
  })();
3442
3945
 
3443
- IR.prototype.inspect = function(indent) {
3946
+ Core.prototype.inspect = function(indent) {
3444
3947
  if (indent == null) {
3445
3948
  indent = 0;
3446
3949
  }
@@ -3511,50 +4014,155 @@ Gibbon.IR = IR = (function(_super) {
3511
4014
  fail: function(m) {
3512
4015
  return "(FAIL " + m + ")";
3513
4016
  },
3514
- binop: function(op, l, r) {
3515
- return "(`" + op + "` " + (l.inspect()) + " " + (r.inspect()) + ")";
4017
+ op1: function(op, arg) {
4018
+ return "(`" + op + "` " + (arg.inspect()) + ")";
3516
4019
  },
3517
- extern: function(name, val) {
3518
- return "(`" + name + "` " + (val.inspect()) + ")";
4020
+ op2: function(op, l, r) {
4021
+ return "(`" + op + "` " + (l.inspect()) + " " + (r.inspect()) + ")";
3519
4022
  },
3520
4023
  rescue: function(e, d) {
3521
4024
  return "(RESCUE " + (e.inspect()) + " " + (d.inspect()) + ")";
3522
4025
  },
3523
4026
  constant: function(v) {
3524
4027
  return '' + v;
4028
+ },
4029
+ next: function() {
4030
+ return 'NEXT';
3525
4031
  }
3526
4032
  });
3527
4033
  };
3528
4034
 
3529
- return IR;
4035
+ Core.prototype.hash = function() {
4036
+ var mkVar, r,
4037
+ _this = this;
4038
+ mkVar = (function() {
4039
+ var i;
4040
+ i = 0;
4041
+ return function() {
4042
+ return Core.variable("" + (i += 1));
4043
+ };
4044
+ })();
4045
+ return this.__hash__ || (this.__hash__ = (r = function(el) {
4046
+ return el.cases({
4047
+ global: function() {
4048
+ return '$';
4049
+ },
4050
+ branch: function(cond, ifTrue, ifFalse) {
4051
+ return "(IF " + (r(cond)) + " " + (r(ifTrue)) + " " + (r(ifFalse)) + ")";
4052
+ },
4053
+ bind: function(name, val, expr) {
4054
+ expr = expr.subst(name, mkVar());
4055
+ return "(LET " + (r(val)) + " " + (r(expr)) + ")";
4056
+ },
4057
+ variable: function(name) {
4058
+ return name;
4059
+ },
4060
+ delist: function(e, i) {
4061
+ return "([" + (r(i)) + "] " + (r(e)) + ")";
4062
+ },
4063
+ depair: function(e, k) {
4064
+ return "([" + k + "] " + (r(e)) + ")";
4065
+ },
4066
+ list: function(els) {
4067
+ var e;
4068
+ return "(LIST " + (((function() {
4069
+ var _i, _len, _results;
4070
+ _results = [];
4071
+ for (_i = 0, _len = els.length; _i < _len; _i++) {
4072
+ e = els[_i];
4073
+ _results.push(r(e));
4074
+ }
4075
+ return _results;
4076
+ })()).join(' ')) + ")";
4077
+ },
4078
+ len: function(e) {
4079
+ return "(LEN " + (r(e)) + ")";
4080
+ },
4081
+ mapList: function(list, arg, i, body) {
4082
+ body = body.subst(arg, mkVar()).subst(i, mkVar());
4083
+ return "(MAP " + (r(list)) + " " + (r(body)) + ")";
4084
+ },
4085
+ foldList: function(list, out, arg, next, i, body) {
4086
+ body = body.subst(arg, mkVar()).subst(next, mkVar()).subst(i, mkVar());
4087
+ return "(FOLDR " + (r(list)) + " " + (r(out)) + " " + (r(body)) + ")";
4088
+ },
4089
+ filterList: function(list, arg, body) {
4090
+ body = body.subst(arg, mkVar());
4091
+ return "(FILTER " + (r(list)) + " " + (r(body)) + ")";
4092
+ },
4093
+ zipLists: function(x, y) {
4094
+ return "(ZIP " + (r(x)) + " " + (r(y)) + ")";
4095
+ },
4096
+ squishList: function(l) {
4097
+ return "(SQUISH " + (r(l)) + ")";
4098
+ },
4099
+ pair: function(x, y) {
4100
+ return "(PAIR " + (r(x)) + " " + (r(y)) + ")";
4101
+ },
4102
+ query: function(e, a) {
4103
+ return "(QUERY " + (JSON.stringify(a)) + " " + (r(e)) + ")";
4104
+ },
4105
+ block: function(arg, body) {
4106
+ body = body.subst(arg, mkVar());
4107
+ return "(LAMBDA " + (r(body)) + ")";
4108
+ },
4109
+ app: function(b, a) {
4110
+ return "(APPLY " + (r(b)) + " " + (r(a)) + ")";
4111
+ },
4112
+ localQuery: function(k) {
4113
+ return "@" + k;
4114
+ },
4115
+ fail: function(m) {
4116
+ return "(FAIL)";
4117
+ },
4118
+ op1: function(op, arg) {
4119
+ return "(OP1 " + op + " " + (r(arg)) + ")";
4120
+ },
4121
+ op2: function(op, lhs, rhs) {
4122
+ return "(OP2 " + op + " " + (r(lhs)) + " " + (r(rhs)) + ")";
4123
+ },
4124
+ rescue: function(e, d) {
4125
+ return "(RESCUE " + (r(e)) + " " + (r(d)) + ")";
4126
+ },
4127
+ constant: function(v) {
4128
+ return '' + v;
4129
+ },
4130
+ next: function() {
4131
+ return 'NEXT';
4132
+ }
4133
+ });
4134
+ })(this));
4135
+ };
4136
+
4137
+ return Core;
3530
4138
 
3531
- })(Union);
4139
+ })(Variant);
3532
4140
 
3533
- Gibbon.compile = (function() {
3534
- var compile;
3535
- compile = function(semantic, input, context) {
4141
+ Gibbon.translate = (function() {
4142
+ var translate;
4143
+ translate = function(semantic, input, context) {
3536
4144
  if (input == null) {
3537
- input = IR.global();
4145
+ input = Core.global();
3538
4146
  }
3539
4147
  if (context == null) {
3540
4148
  context = input;
3541
4149
  }
3542
4150
  return semantic.cases({
3543
4151
  definition: function(_, flow) {
3544
- return compile(flow, input, context);
4152
+ return translate(flow, input, context);
3545
4153
  },
3546
4154
  flow: function(_, head, tail) {
3547
4155
  if (tail) {
3548
- return compile(head, compile(tail, input, context), context);
4156
+ return translate(head, translate(tail, input, context), context);
3549
4157
  } else {
3550
- return compile(head, input, context);
4158
+ return translate(head, input, context);
3551
4159
  }
3552
4160
  },
3553
4161
  query: function(annotations) {
3554
4162
  return input.query(annotations);
3555
4163
  },
3556
4164
  localAccessor: function(key) {
3557
- return IR.localQuery(key);
4165
+ return Core.localQuery(key);
3558
4166
  },
3559
4167
  func: function(name, args, tvars) {
3560
4168
  var arg, compArgs;
@@ -3563,23 +4171,23 @@ Gibbon.compile = (function() {
3563
4171
  _results = [];
3564
4172
  for (_i = 0, _len = args.length; _i < _len; _i++) {
3565
4173
  arg = args[_i];
3566
- _results.push(compile(arg, context));
4174
+ _results.push(translate(arg, context));
3567
4175
  }
3568
4176
  return _results;
3569
4177
  })();
3570
4178
  return stdlib[name].compile(input, compArgs, tvars);
3571
4179
  },
3572
4180
  literal: function(syntax) {
3573
- return IR.constant(syntax.value);
4181
+ return Core.constant(syntax.value);
3574
4182
  },
3575
4183
  list: function(elements, squish) {
3576
4184
  var e, list;
3577
- list = IR.list((function() {
4185
+ list = Core.list((function() {
3578
4186
  var _i, _len, _results;
3579
4187
  _results = [];
3580
4188
  for (_i = 0, _len = elements.length; _i < _len; _i++) {
3581
4189
  e = elements[_i];
3582
- _results.push(compile(e, context));
4190
+ _results.push(translate(e, context));
3583
4191
  }
3584
4192
  return _results;
3585
4193
  })());
@@ -3590,28 +4198,25 @@ Gibbon.compile = (function() {
3590
4198
  }
3591
4199
  },
3592
4200
  pair: function(x, y) {
3593
- return IR.pair(compile(x, context), compile(y, context));
4201
+ return Core.pair(translate(x, context), translate(y, context));
3594
4202
  },
3595
4203
  block: function(body) {
3596
4204
  var arg;
3597
- arg = IR.makeVariable('arg');
3598
- return IR.block(arg.name, compile(body, arg, arg));
4205
+ arg = Core.makeVariable('arg');
4206
+ return Core.block(arg.name, translate(body, arg, arg));
3599
4207
  },
3600
4208
  defaulted: function(body, alternative) {
3601
- return IR.rescue(compile(body, context), compile(alternative, context));
4209
+ return Core.rescue(translate(body, context), translate(alternative, context));
3602
4210
  }
3603
4211
  });
3604
4212
  };
3605
4213
  return function(semantic) {
3606
- return compile(semantic);
4214
+ return translate(semantic);
3607
4215
  };
3608
4216
  })();
3609
- // Generated by CoffeeScript 1.6.3
4217
+
3610
4218
  Gibbon.optimize = (function() {
3611
4219
  var insertBindings, partialEval;
3612
- insertBindings = function(expr) {
3613
- return expr;
3614
- };
3615
4220
  partialEval = function(expr) {
3616
4221
  return expr.cases({
3617
4222
  depair: function(expr, key) {
@@ -3665,7 +4270,7 @@ Gibbon.optimize = (function() {
3665
4270
  return this.branch(ifTrue, ifFalse);
3666
4271
  }
3667
4272
  },
3668
- extern: function(name, arg) {
4273
+ op1: function(name, arg) {
3669
4274
  if (name === '!') {
3670
4275
 
3671
4276
  return partialEval(arg.branch(ifFalse, ifTrue));
@@ -3681,6 +4286,7 @@ Gibbon.optimize = (function() {
3681
4286
  app: function(block, arg) {
3682
4287
  return partialEval(block).cases({
3683
4288
  block: function(argName, body) {
4289
+
3684
4290
  return partialEval(body.subst(argName, arg));
3685
4291
  },
3686
4292
  other: function() {
@@ -3696,7 +4302,7 @@ Gibbon.optimize = (function() {
3696
4302
  for (i = _i = 0, _len = elements.length; _i < _len; i = ++_i) {
3697
4303
  el = elements[i];
3698
4304
 
3699
- partialEval(body.subst(arg, el).subst(ixName, IR.constant(i)));
4305
+ partialEval(body.subst(arg, el).subst(ixName, Core.constant(i)));
3700
4306
  }
3701
4307
  mapped = (function() {
3702
4308
  var _j, _len1, _results;
@@ -3707,10 +4313,16 @@ Gibbon.optimize = (function() {
3707
4313
  }
3708
4314
  return _results;
3709
4315
  })();
3710
- return partialEval(IR.list(mapped));
4316
+ return partialEval(Core.list(mapped));
4317
+ },
4318
+ mapList: function(innerList, innerArg, innerIdxArg, innerBody) {
4319
+ var newBody;
4320
+
4321
+ newBody = body.subst(arg, innerBody).subst(ixName, Core.variable(innerIdxArg));
4322
+ return Core.mapList(innerList, innerArg, innerIdxArg, newBody);
3711
4323
  },
3712
4324
  other: function() {
3713
- return IR.mapList(this, arg, ixName, partialEval(body));
4325
+ return Core.mapList(this, arg, ixName, partialEval(body));
3714
4326
  }
3715
4327
  });
3716
4328
  },
@@ -3726,12 +4338,18 @@ Gibbon.optimize = (function() {
3726
4338
  }
3727
4339
  next = _loop(i + 1);
3728
4340
 
3729
- return body.subst(arg, elements[i]).subst(accum, next).subst(ixName, IR.constant(i));
4341
+ return body.subst(arg, elements[i]).subst(accum, next).subst(ixName, Core.constant(i));
3730
4342
  };
3731
4343
  return partialEval(_loop(0));
3732
4344
  },
4345
+ mapList: function(innerList, innerArg, innerIdxArg, mapBody) {
4346
+ var newBody;
4347
+
4348
+ newBody = body.subst(arg, mapBody).subst(ixName, Core.variable(innerIdxArg));
4349
+ return Core.foldList(innerList, out, innerArg, accum, innerIdxArg, newBody);
4350
+ },
3733
4351
  other: function() {
3734
- return IR.foldList(this, partialEval(out), arg, accum, ixName, partialEval(body));
4352
+ return Core.foldList(this, partialEval(out), arg, accum, ixName, partialEval(body));
3735
4353
  }
3736
4354
  });
3737
4355
  },
@@ -3739,7 +4357,7 @@ Gibbon.optimize = (function() {
3739
4357
  return partialEval(list).cases({
3740
4358
  list: function(elements) {
3741
4359
 
3742
- return IR.constant(elements.length);
4360
+ return Core.constant(elements.length);
3743
4361
  },
3744
4362
  mapList: function(list) {
3745
4363
 
@@ -3759,146 +4377,58 @@ Gibbon.optimize = (function() {
3759
4377
  l = partialEval(l);
3760
4378
  r = partialEval(r);
3761
4379
  if (!(l._tag === 'list' && r._tag === 'list')) {
3762
- return IR.zipLists(l, r);
4380
+ return Core.zipLists(l, r);
3763
4381
  }
3764
4382
  elements = (function() {
3765
4383
  var _i, _len, _results;
3766
4384
  _results = [];
3767
4385
  for (i = _i = 0, _len = l.length; _i < _len; i = ++_i) {
3768
4386
  e = l[i];
3769
- _results.push(IR.pair(e, r[i]));
4387
+ _results.push(Core.pair(e, r[i]));
3770
4388
  }
3771
4389
  return _results;
3772
4390
  })();
3773
- return partialEval(IR.list(elements));
4391
+ return partialEval(Core.list(elements));
3774
4392
  },
3775
- extern: function(op, arg) {
4393
+ op1: function(op, arg) {
3776
4394
  return partialEval(arg).cases({
3777
4395
  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
- })());
4396
+ return Core.constant(applyOp1(op, value));
3791
4397
  },
3792
4398
  other: function() {
3793
- return IR.extern(op, this);
4399
+ return Core.op1(op, this);
3794
4400
  }
3795
4401
  });
3796
4402
  },
3797
- binop: function(op, left, right) {
3798
- var l, r, _ref;
4403
+ op2: function(op, left, right) {
4404
+ var checkIdent, identFold, l, r, _ref14;
3799
4405
  left = partialEval(left);
3800
4406
  right = partialEval(right);
4407
+ checkIdent = function(opTest, val, ident, identVal) {
4408
+ return op === opTest && val._tag === 'constant' && val.value === ident && identVal;
4409
+ };
4410
+ identFold = checkIdent('*', right, 1, left) || checkIdent('*', left, 1, right) || checkIdent('*', right, 0, Core.constant(0)) || checkIdent('*', left, 0, Core.constant(0)) || checkIdent('+', left, 0, right) || checkIdent('+', right, 0, left) || checkIdent('/', right, 1, left);
4411
+ if (identFold) {
4412
+
4413
+ return identFold;
4414
+ }
3801
4415
  if (!(left._tag === 'constant' && right._tag === 'constant')) {
3802
- return IR.binop(op, left, right);
4416
+ return Core.op2(op, left, right);
3803
4417
  }
3804
4418
 
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
- })());
4419
+ _ref14 = [left.value, right.value], l = _ref14[0], r = _ref14[1];
4420
+ return Core.constant(applyOp2(op, left.value, right.value));
3828
4421
  },
3829
4422
  rescue: function(expr, default_) {
3830
- var alwaysFails, alwaysSucceeds;
3831
4423
  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)) {
4424
+ if (expr.alwaysSucceeds()) {
3895
4425
 
3896
4426
  return expr;
3897
- } else if (alwaysFails(expr)) {
4427
+ } else if (expr.alwaysFails()) {
3898
4428
 
3899
4429
  return partialEval(default_);
3900
4430
  } else {
3901
- return IR.rescue(expr, partialEval(default_));
4431
+ return Core.rescue(expr, partialEval(default_));
3902
4432
  }
3903
4433
  },
3904
4434
  other: function() {
@@ -3906,6 +4436,79 @@ Gibbon.optimize = (function() {
3906
4436
  }
3907
4437
  });
3908
4438
  };
4439
+ insertBindings = (function() {
4440
+ var findLastCommon, genSubstitutions, makeCrumbs, simplify;
4441
+ makeCrumbs = function(trace) {
4442
+ return trace.mapArray(function(e) {
4443
+ return e.hash();
4444
+ }).reverse();
4445
+ };
4446
+ genSubstitutions = function(expr) {
4447
+ var newTrace, queue, sub, substitutions, trace, _i, _len, _ref14, _ref15;
4448
+ substitutions = new ObjHash;
4449
+ queue = [[expr, List.empty()]];
4450
+ while (queue.length) {
4451
+ _ref14 = queue.shift(), expr = _ref14[0], trace = _ref14[1];
4452
+ if (expr.isSimple()) {
4453
+ continue;
4454
+ }
4455
+ if (substitutions.has(expr)) {
4456
+ substitutions.get(expr).push(makeCrumbs(trace));
4457
+ } else {
4458
+ substitutions.set(expr, [makeCrumbs(trace)]);
4459
+ newTrace = trace.cons(expr);
4460
+ _ref15 = expr.subtrees();
4461
+ for (_i = 0, _len = _ref15.length; _i < _len; _i++) {
4462
+ sub = _ref15[_i];
4463
+ queue.push([sub, newTrace]);
4464
+ }
4465
+ }
4466
+ }
4467
+ return substitutions;
4468
+ };
4469
+ findLastCommon = function(_arg) {
4470
+ var i, last, refCrumb, reference, rest, testCrumbs, _i, _j, _len, _len1;
4471
+ reference = _arg[0], rest = 2 <= _arg.length ? __slice.call(_arg, 1) : [];
4472
+ last = null;
4473
+ for (i = _i = 0, _len = reference.length; _i < _len; i = ++_i) {
4474
+ refCrumb = reference[i];
4475
+ for (_j = 0, _len1 = rest.length; _j < _len1; _j++) {
4476
+ testCrumbs = rest[_j];
4477
+ if (refCrumb !== testCrumbs[i]) {
4478
+ return last;
4479
+ }
4480
+ }
4481
+ last = refCrumb;
4482
+ }
4483
+ return refCrumb;
4484
+ };
4485
+ simplify = function(expr, substitutions) {
4486
+ substitutions.each(function(subExpr, crumbs) {
4487
+ var lastCommon, name, recurse;
4488
+ if (!(crumbs.length >= 2)) {
4489
+ return;
4490
+ }
4491
+ lastCommon = findLastCommon(crumbs);
4492
+ name = nameGen('b');
4493
+
4494
+ return expr = (recurse = function(expr) {
4495
+ var hash;
4496
+ hash = expr.hash();
4497
+ if (hash === lastCommon) {
4498
+ return Core.bind(name, subExpr, expr.map(recurse));
4499
+ } else if (hash === subExpr.hash()) {
4500
+ return Core.variable(name);
4501
+ } else {
4502
+ return expr.map(recurse);
4503
+ }
4504
+ })(expr);
4505
+ });
4506
+ return expr;
4507
+ };
4508
+ return function(expr) {
4509
+ return simplify(expr, genSubstitutions(expr));
4510
+ };
4511
+ })();
3909
4512
  return function(expr) {
3910
4513
  var out;
3911
4514
 
@@ -3914,54 +4517,1909 @@ Gibbon.optimize = (function() {
3914
4517
  return out;
3915
4518
  };
3916
4519
  })();
3917
- // Generated by CoffeeScript 1.6.3
3918
- var stdlib;
3919
4520
 
3920
- stdlib = Gibbon.stdlib = (function() {
3921
- var compEquals, iFalse, iTrue, vFalse, vTrue;
3922
- vTrue = Value.boolean(true);
3923
- vFalse = Value.boolean(false);
3924
- iTrue = IR.constant(true);
3925
- iFalse = IR.constant(false);
3926
- compEquals = function(x, y, type) {
3927
- var direct;
3928
- direct = function() {
3929
- return x.binop('===', y);
3930
- };
3931
- return type.cases({
3932
- entity: direct,
3933
- numeric: direct,
3934
- string: direct,
3935
- bool: direct,
3936
- block: function() {
3937
- return iFalse;
4521
+ nameGen = (function(i) {
4522
+ return function(name) {
4523
+ return "" + name + (i += 1);
4524
+ };
4525
+ })(0);
4526
+
4527
+ Step = (function(_super) {
4528
+ __extends(Step, _super);
4529
+
4530
+ function Step() {
4531
+ _ref14 = Step.__super__.constructor.apply(this, arguments);
4532
+ return _ref14;
4533
+ }
4534
+
4535
+ Step.variants({
4536
+ "let": ['lval', 'value', 'expr'],
4537
+ fork: ['forks'],
4538
+ each: ['length', 'cont'],
4539
+ letCont: ['name', 'args', 'body', 'expr'],
4540
+ letJoin: ['name', 'order', 'cont', 'expr'],
4541
+ next: ['cont', 'args'],
4542
+ app: ['fn', 'args', 'rescue', 'next'],
4543
+ query: ['annotations', 'arg', 'rescue', 'next'],
4544
+ localQuery: ['key', 'rescue', 'next'],
4545
+ "if": ['cond', 'trueCont', 'falseCont']
4546
+ });
4547
+
4548
+ Step.prototype.inspect = function() {
4549
+ return this.cases({
4550
+ "let": function(varName, value, expr) {
4551
+ return "(LET " + varName + " " + (value.inspect()) + " " + (expr.inspect()) + ")";
3938
4552
  },
3939
- pair: function() {
3940
- var eqx, eqy;
3941
- eqx = compEquals(input.depair('first'), input.depair('second'), type.first);
3942
- eqy = compEquals(input.depair('first'), input.depair('second'), type.first);
3943
- return eqx.branch(eqy, iFalse);
4553
+ letCont: function(name, args, body, expr) {
4554
+ return "(LETC " + name + " \\" + (args.join(',')) + " " + (body.inspect()) + " " + (expr.inspect()) + ")";
3944
4555
  },
3945
- list: function() {
3946
- var eqEls, eqLength;
3947
- eqLength = compEquals(x.len(), y.len(), Type.numeric());
3948
- eqEls = x.zipList(y).foldList(iTrue, function(pair) {
3949
- return compEquals(pair.depair('first'), pair.depair('second'), type.of);
3950
- });
3951
- return eqLength.branch(eqEls, iFalse);
4556
+ fork: function(forks) {
4557
+ var f;
4558
+ return "(FORK " + (((function() {
4559
+ var _i, _len, _results;
4560
+ _results = [];
4561
+ for (_i = 0, _len = forks.length; _i < _len; _i++) {
4562
+ f = forks[_i];
4563
+ _results.push("->" + f);
4564
+ }
4565
+ return _results;
4566
+ })()).join(' ')) + ")";
4567
+ },
4568
+ each: function(list, cont) {
4569
+ return "(EACH " + list + " ->" + cont + ")";
4570
+ },
4571
+ letJoin: function(name, order, cont, expr) {
4572
+ return "(LETJ " + name + "/" + order + " ->" + cont + " " + (expr.inspect()) + ")";
4573
+ },
4574
+ letMap: function(name, list, joinName, arg, body, expr) {
4575
+ return "(LETM " + name + " " + list + " " + joinName + "<- \\" + arg + " " + (body.inspect()) + " " + (expr.inspect()) + ")";
4576
+ },
4577
+ next: function(cont, args) {
4578
+ return "(->" + cont + " " + (args.join(' ')) + ")";
4579
+ },
4580
+ app: function(fn, args, rescue, next) {
4581
+ return "(" + fn + " " + (args.join(' ')) + " !->" + rescue + " ->" + next + ")";
4582
+ },
4583
+ query: function(annotations, arg, rescue, next) {
4584
+ return "(Q " + (JSON.stringify(annotations)) + " !->" + rescue + " ->" + next + ")";
4585
+ },
4586
+ localQuery: function(key, rescue, next) {
4587
+ return "(@" + key + " !->" + rescue + ", ->" + next + ")";
4588
+ },
4589
+ "if": function(cond, trueCont, falseCont) {
4590
+ return "(IF " + cond + " ->" + trueCont + " ->" + falseCont + ")";
3952
4591
  }
3953
4592
  });
3954
4593
  };
3955
- return {
3956
- "case": {
3957
- type: parse.type('case [bool : %b] = % -> %b'),
3958
- compile: function(_, _arg) {
3959
- var alts;
3960
- alts = _arg[0];
3961
- return alts.foldList(IR.fail('non-exhaustive cases'), function(el, next) {
3962
- return el.depair('first').branch(el.depair('second'), next);
3963
- });
3964
- }
4594
+
4595
+ Step.prototype.inspectLines = function(indent) {
4596
+ var i;
4597
+ if (indent == null) {
4598
+ indent = 0;
4599
+ }
4600
+ i = new Array(indent + 1).join(' ');
4601
+ return this.cases({
4602
+ "let": function(varName, value, expr) {
4603
+ return "" + i + "let " + varName + " = " + (value.inspect()) + "\n" + (expr.inspectLines(indent));
4604
+ },
4605
+ letCont: function(name, args, body, expr) {
4606
+ return "" + i + name + " " + (args.join(' ')) + ":\n" + (body.inspectLines(indent + 1)) + "\n" + (expr.inspectLines(indent));
4607
+ },
4608
+ letJoin: function(name, order, cont, expr) {
4609
+ return "" + i + name + ": join/" + order + " ->" + cont + "\n" + (expr.inspectLines(indent));
4610
+ },
4611
+ fork: function(conts) {
4612
+ var c;
4613
+ return "" + i + "fork " + (((function() {
4614
+ var _i, _len, _results;
4615
+ _results = [];
4616
+ for (_i = 0, _len = conts.length; _i < _len; _i++) {
4617
+ c = conts[_i];
4618
+ _results.push("->" + c);
4619
+ }
4620
+ return _results;
4621
+ })()).join(' '));
4622
+ },
4623
+ each: function(length, cont) {
4624
+ return "" + i + "each/" + length + " ->" + cont;
4625
+ },
4626
+ map: function(name, list, arg, joinName, body, expr) {
4627
+ return "" + i + name + " = map " + list + " \\" + joinName + ":\n" + (body.inspectLines(indent + 1)) + "\n" + (expr.inspectLines(indent));
4628
+ },
4629
+ next: function(name, args) {
4630
+ return "" + i + "go " + name + " " + (args.join(' '));
4631
+ },
4632
+ app: function(fn, args, rescue, next) {
4633
+ return "" + i + "app " + fn + " " + (args.join(' ')) + " !->" + rescue + " ->" + next;
4634
+ },
4635
+ query: function(annotations, arg, rescue, next) {
4636
+ return "" + i + "query " + (JSON.stringify(annotations)) + " " + arg + " !->" + rescue + " ->" + next;
4637
+ },
4638
+ localQuery: function(key, rescue, next) {
4639
+ return "" + i + "@" + key + " !->" + rescue + ", ->" + next;
4640
+ },
4641
+ "if": function(cond, trueCont, falseCont) {
4642
+ return "" + i + "if " + cond + " ->" + trueCont + " else ->" + falseCont;
4643
+ }
4644
+ });
4645
+ };
4646
+
4647
+ Step.prototype.search = function(f) {
4648
+ if (f(this)) {
4649
+ return true;
4650
+ }
4651
+ return this.cases({
4652
+ "let": function(_, __, expr) {
4653
+ return expr.search(f);
4654
+ },
4655
+ project: function(_, __, ___, expr) {
4656
+ return expr.search(f);
4657
+ },
4658
+ letCont: function(_, __, body, expr) {
4659
+ return body.search(f) || expr.search(f);
4660
+ },
4661
+ letJoin: function(_, __, ___, expr) {
4662
+ return expr.search(f);
4663
+ },
4664
+ other: function() {
4665
+ return false;
4666
+ }
4667
+ });
4668
+ };
4669
+
4670
+ Step.prototype.map = function(f) {
4671
+ return this.cases({
4672
+ "let": function(lval, value, expr) {
4673
+ return Step["let"](lval, value.mapSteps(f), f(expr));
4674
+ },
4675
+ project: function(lval, value, index, expr) {
4676
+ return Step.project(lval, value, index, f(expr));
4677
+ },
4678
+ letCont: function(name, args, body, expr) {
4679
+ return Step.letCont(name, args, f(body), f(expr));
4680
+ },
4681
+ other: function() {
4682
+ return this;
4683
+ }
4684
+ });
4685
+ };
4686
+
4687
+ Step.prototype.hasFree = function(varName) {
4688
+ return this.search(function(step) {
4689
+ return step.cases({
4690
+ "let": function(_, value, __) {
4691
+ return value.hasFree(varName);
4692
+ },
4693
+ next: function(cont, args) {
4694
+ return varName === cont || __indexOf.call(args, varName) >= 0;
4695
+ },
4696
+ app: function(fn, args, rescue, next) {
4697
+ return (varName === fn || varName === rescue || varName === next) || __indexOf.call(args, varName) >= 0;
4698
+ },
4699
+ query: function(annotations, arg, rescue, next) {
4700
+ return varName === arg || varName === rescue || varName === next;
4701
+ },
4702
+ localQuery: function(key, rescue, next) {
4703
+ return varName === rescue || varName === next;
4704
+ },
4705
+ "if": function(cond, trueCont, falseCont) {
4706
+ return varName === cond || varName === trueCont || varName === falseCont;
4707
+ },
4708
+ letJoin: function(name, order, cont) {
4709
+ return varName === order || varName === cont;
4710
+ },
4711
+ fork: function(conts) {
4712
+ return __indexOf.call(conts, varName) >= 0;
4713
+ },
4714
+ each: function(name, cont) {
4715
+ return varName === name || varName === cont;
4716
+ },
4717
+ other: function() {
4718
+ return false;
4719
+ }
4720
+ });
4721
+ });
4722
+ };
4723
+
4724
+ Step.prototype.genTrace = function(contName) {
4725
+ return this.cases({
4726
+ "let": function(_, __, e) {
4727
+ return e.genTrace(contName);
4728
+ },
4729
+ letCont: function(_, __, body, expr) {
4730
+ return body.genTrace(contName).concat(expr.genTrace(contName));
4731
+ },
4732
+ letJoin: function(_, __, cont, e) {
4733
+ var calls;
4734
+ calls = e.genTrace(contName);
4735
+ if (cont === contName) {
4736
+ return [ContinuationTrace.simple(this)].concat(calls);
4737
+ } else {
4738
+ return calls;
4739
+ }
4740
+ },
4741
+ app: function(_, __, rescue, next) {
4742
+ if (contName === rescue) {
4743
+ return [ContinuationTrace.key(this, 'rescue')];
4744
+ } else if (contName === next) {
4745
+ return [ContinuationTrace.key(this, 'next')];
4746
+ } else {
4747
+ return [];
4748
+ }
4749
+ },
4750
+ query: function(_, __, rescue, next) {
4751
+ if (contName === rescue) {
4752
+ return [ContinuationTrace.key(this, 'rescue')];
4753
+ } else if (contName === next) {
4754
+ return [ContinuationTrace.key(this, 'next')];
4755
+ } else {
4756
+ return [];
4757
+ }
4758
+ },
4759
+ localQuery: function(_, rescue, next) {
4760
+ if (contName === rescue) {
4761
+ return [ContinuationTrace.key(this, 'rescue')];
4762
+ } else if (contName === next) {
4763
+ return [ContinuationTrace.key(this, 'next')];
4764
+ } else {
4765
+ return [];
4766
+ }
4767
+ },
4768
+ next: function(name, _) {
4769
+ if (contName === name) {
4770
+ return [ContinuationTrace.simple(this)];
4771
+ } else {
4772
+ return [];
4773
+ }
4774
+ },
4775
+ fork: function(forks) {
4776
+ var fork, i, _i, _len;
4777
+ for (i = _i = 0, _len = forks.length; _i < _len; i = ++_i) {
4778
+ fork = forks[i];
4779
+ if (contName === fork) {
4780
+ return [ContinuationTrace.index(this, 'forks', i)];
4781
+ }
4782
+ }
4783
+ return [];
4784
+ },
4785
+ each: function(length, cont) {
4786
+ if (contName === cont) {
4787
+ return [ContinuationTrace.key(this, 'cont')];
4788
+ } else {
4789
+ return [];
4790
+ }
4791
+ },
4792
+ "if": function(_, trueCont, falseCont) {
4793
+ if (contName === trueCont) {
4794
+ return [ContinuationTrace.key(this, 'trueCont')];
4795
+ } else if (contName === falseCont) {
4796
+ return [ContinuationTrace.key(this, 'falseCont')];
4797
+ } else {
4798
+ return [];
4799
+ }
4800
+ }
4801
+ });
4802
+ };
4803
+
4804
+ Step.prototype.subst = function(varName, target) {
4805
+ return this.cases({
4806
+ "let": function(lval, value, expr) {
4807
+ value = value.subst(varName, target);
4808
+ if (varName === lval) {
4809
+ return Step["let"](lval, value, expr);
4810
+ }
4811
+ return Step["let"](lval, value, expr.subst(varName, target));
4812
+ },
4813
+ letJoin: function(name, order, cont, expr) {
4814
+ if (order === varName) {
4815
+ order = target;
4816
+ }
4817
+ if (cont === varName) {
4818
+ cont = target;
4819
+ }
4820
+ expr = expr.subst(varName, target);
4821
+ return Step.letJoin(name, order, cont, expr);
4822
+ },
4823
+ next: function(cont, args) {
4824
+ var arg, newArgs;
4825
+ if (cont === varName) {
4826
+ cont = target;
4827
+ }
4828
+ newArgs = (function() {
4829
+ var _i, _len, _results;
4830
+ _results = [];
4831
+ for (_i = 0, _len = args.length; _i < _len; _i++) {
4832
+ arg = args[_i];
4833
+ if (arg === varName) {
4834
+ _results.push(target);
4835
+ } else {
4836
+ _results.push(arg);
4837
+ }
4838
+ }
4839
+ return _results;
4840
+ })();
4841
+ return Step.next(cont, newArgs);
4842
+ },
4843
+ app: function(fn, args, rescue, next) {
4844
+ var arg;
4845
+ if (fn === varName) {
4846
+ fn = target;
4847
+ }
4848
+ if (rescue === varName) {
4849
+ rescue = target;
4850
+ }
4851
+ if (next === varName) {
4852
+ next = target;
4853
+ }
4854
+ args = (function() {
4855
+ var _i, _len, _results;
4856
+ _results = [];
4857
+ for (_i = 0, _len = args.length; _i < _len; _i++) {
4858
+ arg = args[_i];
4859
+ if (arg === varName) {
4860
+ _results.push(target);
4861
+ } else {
4862
+ _results.push(arg);
4863
+ }
4864
+ }
4865
+ return _results;
4866
+ })();
4867
+ return Step.app(fn, args, rescue, next);
4868
+ },
4869
+ query: function(annotations, arg, rescue, next) {
4870
+ if (arg === varName) {
4871
+ arg = target;
4872
+ }
4873
+ if (rescue === varName) {
4874
+ rescue = target;
4875
+ }
4876
+ if (next === varName) {
4877
+ next = target;
4878
+ }
4879
+ return Step.query(annotations, arg, rescue, next);
4880
+ },
4881
+ localQuery: function(key, rescue, next) {
4882
+ if (rescue === varName) {
4883
+ rescue = target;
4884
+ }
4885
+ if (next === varName) {
4886
+ next = target;
4887
+ }
4888
+ return Step.localQuery(key, rescue, next);
4889
+ },
4890
+ "if": function(cond, trueCont, falseCont) {
4891
+ if (cond === varName) {
4892
+ cond = target;
4893
+ }
4894
+ if (trueCont === varName) {
4895
+ trueCont = target;
4896
+ }
4897
+ if (falseCont === varName) {
4898
+ falseCont = target;
4899
+ }
4900
+ return Step["if"](cond, trueCont, falseCont);
4901
+ },
4902
+ each: function(name, cont) {
4903
+ if (name === varName) {
4904
+ name = target;
4905
+ }
4906
+ if (cont === varName) {
4907
+ cont = target;
4908
+ }
4909
+ return Step.each(name, cont);
4910
+ },
4911
+ other: function() {
4912
+ return this.map(function(x) {
4913
+ return x.subst(varName, target);
4914
+ });
4915
+ }
4916
+ });
4917
+ };
4918
+
4919
+ Step.prototype.substAll = function(varNames, targets) {
4920
+ var i, replaced, varName, _i, _len;
4921
+ replaced = this;
4922
+ for (i = _i = 0, _len = varNames.length; _i < _len; i = ++_i) {
4923
+ varName = varNames[i];
4924
+ replaced = replaced.subst(varName, targets[i]);
4925
+ }
4926
+ return replaced;
4927
+ };
4928
+
4929
+ Step.makeCont = function(arity, fBody, fExpr) {
4930
+ var contName, varNames;
4931
+ contName = nameGen('k');
4932
+ varNames = (function() {
4933
+ var _i, _results;
4934
+ _results = [];
4935
+ for (_i = 0; 0 <= arity ? _i < arity : _i > arity; 0 <= arity ? _i++ : _i--) {
4936
+ _results.push(nameGen('t'));
4937
+ }
4938
+ return _results;
4939
+ })();
4940
+ return Step.letCont(contName, varNames, fBody.apply(null, varNames), fExpr(contName));
4941
+ };
4942
+
4943
+ Step.makeVar = function(val, f) {
4944
+ var varName;
4945
+ varName = nameGen('t');
4946
+ return Step["let"](varName, val, f(varName));
4947
+ };
4948
+
4949
+ Step.makeJoin = function(order, outCont, f) {
4950
+ var joinName, mkBody;
4951
+ joinName = nameGen('j');
4952
+ mkBody = function(index) {
4953
+ return f(index, function(mappedVal) {
4954
+ return Step.next(joinName, [index, mappedVal]);
4955
+ });
4956
+ };
4957
+ return Step.makeCont(1, mkBody, function(bodyCont) {
4958
+ return Step.letJoin(joinName, order, outCont, Step.each(order, bodyCont));
4959
+ });
4960
+ };
4961
+
4962
+ Step.prototype.extendTrace = function(trace) {
4963
+ return this.cases;
4964
+ };
4965
+
4966
+ Step.prototype.walk = function(f) {
4967
+ var recurse;
4968
+ return (recurse = function(step, trace) {
4969
+ return f(step, trace, function(newStep) {
4970
+ var newTrace;
4971
+ newTrace = trace.extendWith(step);
4972
+ return recurse(newStep, newTrace);
4973
+ });
4974
+ })(this, Trace.empty());
4975
+ };
4976
+
4977
+ return Step;
4978
+
4979
+ })(Variant);
4980
+
4981
+ VarTrace = (function(_super) {
4982
+ __extends(VarTrace, _super);
4983
+
4984
+ function VarTrace() {
4985
+ _ref15 = VarTrace.__super__.constructor.apply(this, arguments);
4986
+ return _ref15;
4987
+ }
4988
+
4989
+ VarTrace.variants({
4990
+ value: ['val'],
4991
+ continued: ['continuation', 'index']
4992
+ });
4993
+
4994
+ VarTrace.prototype.equals = function(other) {
4995
+ if (this._tag !== other._tag) {
4996
+ return false;
4997
+ }
4998
+ return this.cases({
4999
+ value: function(val) {
5000
+ return val.equals(other.val);
5001
+ },
5002
+ continued: function(continuation, index) {
5003
+ return continuation === other.continuation && index === other.index;
5004
+ }
5005
+ });
5006
+ };
5007
+
5008
+ return VarTrace;
5009
+
5010
+ })(Variant);
5011
+
5012
+ ContinuationTrace = (function(_super) {
5013
+ __extends(ContinuationTrace, _super);
5014
+
5015
+ function ContinuationTrace() {
5016
+ _ref16 = ContinuationTrace.__super__.constructor.apply(this, arguments);
5017
+ return _ref16;
5018
+ }
5019
+
5020
+ ContinuationTrace.variants({
5021
+ simple: ['node'],
5022
+ key: ['node', 'key'],
5023
+ index: ['node', 'key', 'index']
5024
+ });
5025
+
5026
+ return ContinuationTrace;
5027
+
5028
+ })(Variant);
5029
+
5030
+ Trace = (function(_super) {
5031
+ __extends(Trace, _super);
5032
+
5033
+ function Trace() {
5034
+ _ref17 = Trace.__super__.constructor.apply(this, arguments);
5035
+ return _ref17;
5036
+ }
5037
+
5038
+ Trace.variants({
5039
+ empty: [],
5040
+ contTrace: ['parent', 'name', 'calls'],
5041
+ varTrace: ['parent', 'name', 'traces']
5042
+ });
5043
+
5044
+ Trace.prototype.boundNames = function() {
5045
+ return this.cases({
5046
+ empty: function() {
5047
+ return [];
5048
+ },
5049
+ contTrace: function(parent, name, _, __) {
5050
+ return parent.boundNames().concat([name]);
5051
+ },
5052
+ varTrace: function(parent, name, _) {
5053
+ return parent.boundNames().concat([name]);
5054
+ }
5055
+ });
5056
+ };
5057
+
5058
+ Trace.prototype.extendWith = function(step) {
5059
+ var _this = this;
5060
+ return step.cases({
5061
+ "let": function(lval, value, _) {
5062
+ return _this.traceVar(lval, VarTrace.value(value));
5063
+ },
5064
+ letCont: function(contName, argNames, body, expr) {
5065
+ var i, name, traced, _i, _len;
5066
+ traced = _this.traceCont(contName, step.genTrace(contName));
5067
+ for (i = _i = 0, _len = argNames.length; _i < _len; i = ++_i) {
5068
+ name = argNames[i];
5069
+ traced = traced.traceVar(name, VarTrace.continued(contName, i));
5070
+ }
5071
+ return traced;
5072
+ },
5073
+ letJoin: function(name) {
5074
+ return _this.traceCont(step.genTrace(name));
5075
+ },
5076
+ other: function() {
5077
+ return _this;
5078
+ }
5079
+ });
5080
+ };
5081
+
5082
+ Trace.prototype.inspect = function() {
5083
+ return "<" + (this.boundNames().join(' ')) + ">";
5084
+ };
5085
+
5086
+ Trace.prototype.traceCont = function(name, traces) {
5087
+ return Trace.contTrace(this, name, traces);
5088
+ };
5089
+
5090
+ Trace.prototype.traceVar = function(name, trace) {
5091
+ return Trace.varTrace(this, name, trace);
5092
+ };
5093
+
5094
+ Trace.prototype.findVarTrace = function(needle) {
5095
+ return this.cases({
5096
+ empty: function() {
5097
+ return null;
5098
+ },
5099
+ contTrace: function(parent) {
5100
+ return parent.findVarTrace(needle);
5101
+ },
5102
+ varTrace: function(parent, name, trace) {
5103
+ if (needle.equals(trace)) {
5104
+ return name;
5105
+ } else {
5106
+ return parent.findVarTrace(needle);
5107
+ }
5108
+ }
5109
+ });
5110
+ };
5111
+
5112
+ Trace.prototype.getVar = function(needle) {
5113
+ return this.cases({
5114
+ empty: function() {
5115
+ throw "no such variable " + needle;
5116
+ },
5117
+ contTrace: function(parent) {
5118
+ return parent.getVar(needle);
5119
+ },
5120
+ varTrace: function(parent, name, subst) {
5121
+ if (name === needle) {
5122
+ return subst;
5123
+ } else {
5124
+ return parent.getVar(needle);
5125
+ }
5126
+ }
5127
+ });
5128
+ };
5129
+
5130
+ Trace.prototype.getCont = function(needle) {
5131
+ return this.cases({
5132
+ empty: function() {
5133
+ throw "no such continuation " + needle;
5134
+ },
5135
+ contTrace: function(parent, name, traces) {
5136
+ if (name === needle) {
5137
+ return traces;
5138
+ } else {
5139
+ return parent.getCont(needle);
5140
+ }
5141
+ },
5142
+ varTrace: function(parent) {
5143
+ return parent.getCont(needle);
5144
+ }
5145
+ });
5146
+ };
5147
+
5148
+ return Trace;
5149
+
5150
+ })(Variant);
5151
+
5152
+ RVal = (function(_super) {
5153
+ __extends(RVal, _super);
5154
+
5155
+ function RVal() {
5156
+ _ref18 = RVal.__super__.constructor.apply(this, arguments);
5157
+ return _ref18;
5158
+ }
5159
+
5160
+ RVal.variants({
5161
+ constant: ['value'],
5162
+ global: [],
5163
+ lambda: ['args', 'rescue', 'next', 'body'],
5164
+ prim: ['arity', 'name', 'args'],
5165
+ list: ['elements'],
5166
+ project: ['val', 'index'],
5167
+ pair: ['first', 'second'],
5168
+ depair: ['val', 'key'],
5169
+ compact: ['val']
5170
+ });
5171
+
5172
+ RVal.prototype.equals = function(other) {
5173
+ if (this._tag !== other._tag) {
5174
+ return false;
5175
+ }
5176
+ return this.cases({
5177
+ constant: function(val) {
5178
+ return val === other.value;
5179
+ },
5180
+ global: function() {
5181
+ return true;
5182
+ },
5183
+ lambda: function() {
5184
+ return false;
5185
+ },
5186
+ prim: function(arity, name, args) {
5187
+ return arity === other.arity && name === other.name && equalArrays(args, other.args);
5188
+ },
5189
+ list: function(elements) {
5190
+ return equalArrays(elements, other.elements);
5191
+ },
5192
+ project: function(val, index) {
5193
+ return val === other.val && index === other.index;
5194
+ },
5195
+ pair: function(first, second) {
5196
+ return first === other.first && second === other.second;
5197
+ },
5198
+ depair: function(val, key) {
5199
+ return val === other.val;
5200
+ },
5201
+ compact: function(val) {
5202
+ return val === other.val;
5203
+ }
5204
+ });
5205
+ };
5206
+
5207
+ RVal.prototype.hasFree = function(varName) {
5208
+ return this.cases({
5209
+ lambda: function(args, rescue, next, body) {
5210
+ return __indexOf.call(args, varName) < 0 && (varName !== rescue && varName !== next) && body.hasFree(varName);
5211
+ },
5212
+ prim: function(_, __, args) {
5213
+ return __indexOf.call(args, varName) >= 0;
5214
+ },
5215
+ list: function(els) {
5216
+ return __indexOf.call(els, varName) >= 0;
5217
+ },
5218
+ project: function(val, index) {
5219
+ return varName === val || varName === index;
5220
+ },
5221
+ pair: function(first, second) {
5222
+ return varName === first || varName === second;
5223
+ },
5224
+ depair: function(val, key) {
5225
+ return varName === val;
5226
+ },
5227
+ compact: function(val) {
5228
+ return val === varName;
5229
+ },
5230
+ other: function() {
5231
+ return false;
5232
+ }
5233
+ });
5234
+ };
5235
+
5236
+ RVal.prototype.subst = function(varName, target) {
5237
+ return this.cases({
5238
+ prim: function(arity, name, args) {
5239
+ var arg, newArgs;
5240
+ newArgs = (function() {
5241
+ var _i, _len, _results;
5242
+ _results = [];
5243
+ for (_i = 0, _len = args.length; _i < _len; _i++) {
5244
+ arg = args[_i];
5245
+ if (arg === varName) {
5246
+ _results.push(target);
5247
+ } else {
5248
+ _results.push(arg);
5249
+ }
5250
+ }
5251
+ return _results;
5252
+ })();
5253
+ return RVal.prim(arity, name, newArgs);
5254
+ },
5255
+ list: function(els) {
5256
+ var el, newEls;
5257
+ newEls = (function() {
5258
+ var _i, _len, _results;
5259
+ _results = [];
5260
+ for (_i = 0, _len = els.length; _i < _len; _i++) {
5261
+ el = els[_i];
5262
+ if (el === varName) {
5263
+ _results.push(target);
5264
+ } else {
5265
+ _results.push(el);
5266
+ }
5267
+ }
5268
+ return _results;
5269
+ })();
5270
+ return RVal.list(newEls);
5271
+ },
5272
+ project: function(val, index) {
5273
+ if (val === varName) {
5274
+ val = target;
5275
+ }
5276
+ if (index === varName) {
5277
+ index = target;
5278
+ }
5279
+ return RVal.project(val, index);
5280
+ },
5281
+ compact: function(val) {
5282
+ if (val === varName) {
5283
+ val = target;
5284
+ }
5285
+ return RVal.compact(val);
5286
+ },
5287
+ pair: function(first, second) {
5288
+ if (first === varName) {
5289
+ first = target;
5290
+ }
5291
+ if (second === varName) {
5292
+ second = target;
5293
+ }
5294
+ return RVal.pair(first, second);
5295
+ },
5296
+ depair: function(val, key) {
5297
+ if (val === varName) {
5298
+ val = target;
5299
+ }
5300
+ return RVal.depair(val, key);
5301
+ },
5302
+ lambda: function() {
5303
+ throw 'TODO';
5304
+ },
5305
+ other: function() {
5306
+ return this;
5307
+ }
5308
+ });
5309
+ };
5310
+
5311
+ RVal.prototype.mapSteps = function() {
5312
+ return this.cases({
5313
+ lambda: function(a, r, n, b) {
5314
+ return RVal.lambda(a, r, n, f(b));
5315
+ },
5316
+ other: function() {
5317
+ return this;
5318
+ }
5319
+ });
5320
+ };
5321
+
5322
+ RVal.makeLambda = function(argNames, f) {
5323
+ var body, nextName, rescueName;
5324
+ rescueName = nameGen('rescue');
5325
+ nextName = nameGen('next');
5326
+ body = f(rescueName, nextName);
5327
+ return RVal.lambda(argNames, rescueName, nextName, body);
5328
+ };
5329
+
5330
+ RVal.prototype.inspect = function() {
5331
+ return this.cases({
5332
+ constant: function(value) {
5333
+ return "" + value;
5334
+ },
5335
+ global: function() {
5336
+ return '$';
5337
+ },
5338
+ prim: function(arity, name, args) {
5339
+ return "(`" + name + "`/" + arity + " " + (args.join(' ')) + ")";
5340
+ },
5341
+ project: function(val, index) {
5342
+ return "" + val + "[" + index + "]";
5343
+ },
5344
+ lambda: function(args, rescue, next, body) {
5345
+ return "(\\" + (args.join(' ')) + " =>" + rescue + " =>" + next + " " + (body.inspect()) + ")";
5346
+ },
5347
+ compact: function(val) {
5348
+ return "(COMPACT " + val + ")";
5349
+ },
5350
+ pair: function(first, second) {
5351
+ return "(PAIR " + first + " " + second + ")";
5352
+ },
5353
+ depair: function(val, key) {
5354
+ return "" + val + "." + key;
5355
+ },
5356
+ list: function(els) {
5357
+ return "[" + (els.join(', ')) + "]";
5358
+ }
5359
+ });
5360
+ };
5361
+
5362
+ return RVal;
5363
+
5364
+ })(Variant);
5365
+
5366
+ Gibbon.sequence = (function() {
5367
+ var bindExprs, sequence, sequenceTail;
5368
+ bindExprs = function(exprs, rescue, f) {
5369
+ var asyncExprs, asyncVars, bindAsyncVars, bound, collectionName, contName, expr, forkBodies, forks, i, idx, joinName, name, recurse, syncExprs, syncStep, syncVars, _, _i, _len;
5370
+ syncExprs = [];
5371
+ asyncExprs = [];
5372
+ bound = [];
5373
+ for (idx = _i = 0, _len = exprs.length; _i < _len; idx = ++_i) {
5374
+ expr = exprs[idx];
5375
+ if (expr.isAsync()) {
5376
+ asyncExprs.push([idx, expr]);
5377
+ } else {
5378
+ syncExprs.push([idx, expr]);
5379
+ }
5380
+ }
5381
+ if (asyncExprs.length === 1) {
5382
+ syncExprs.push(asyncExprs[0]);
5383
+ asyncExprs = [];
5384
+ }
5385
+ syncVars = (function() {
5386
+ var _j, _len1, _ref19, _results;
5387
+ _results = [];
5388
+ for (_j = 0, _len1 = syncExprs.length; _j < _len1; _j++) {
5389
+ _ref19 = syncExprs[_j], idx = _ref19[0], expr = _ref19[1];
5390
+ name = nameGen('t');
5391
+ bound[idx] = name;
5392
+ _results.push(name);
5393
+ }
5394
+ return _results;
5395
+ })();
5396
+ asyncVars = (function() {
5397
+ var _j, _len1, _ref19, _results;
5398
+ _results = [];
5399
+ for (_j = 0, _len1 = asyncExprs.length; _j < _len1; _j++) {
5400
+ _ref19 = asyncExprs[_j], idx = _ref19[0], expr = _ref19[1];
5401
+ _results.push(bound[idx] = nameGen('t'));
5402
+ }
5403
+ return _results;
5404
+ })();
5405
+ syncStep = (recurse = function(i) {
5406
+ var expr_, idx_, _ref19;
5407
+ if (i >= syncExprs.length) {
5408
+ return f(bound);
5409
+ }
5410
+ _ref19 = syncExprs[i], idx_ = _ref19[0], expr_ = _ref19[1];
5411
+ return sequence(expr_, rescue, function(boundExpr) {
5412
+ bound[idx_] = boundExpr;
5413
+ return recurse(i + 1);
5414
+ });
5415
+ })(0);
5416
+ if (!asyncExprs.length) {
5417
+ return syncStep;
5418
+ }
5419
+ joinName = nameGen('j');
5420
+ contName = nameGen('k');
5421
+ forkBodies = (function() {
5422
+ var _j, _len1, _ref19, _results;
5423
+ _results = [];
5424
+ for (i = _j = 0, _len1 = asyncExprs.length; _j < _len1; i = ++_j) {
5425
+ _ref19 = asyncExprs[i], _ = _ref19[0], expr = _ref19[1];
5426
+ _results.push(Step.makeVar(RVal.constant(i), function(boundI) {
5427
+ return sequence(expr, rescue, function(boundExpr) {
5428
+ return Step.next(joinName, [boundI, boundExpr]);
5429
+ });
5430
+ }));
5431
+ }
5432
+ return _results;
5433
+ })();
5434
+ collectionName = nameGen('c');
5435
+ bindAsyncVars = (recurse = function(i) {
5436
+ if (i >= asyncVars.length) {
5437
+ return syncStep;
5438
+ }
5439
+ return sequence(Core.constant(i), rescue, function(boundIndex) {
5440
+ var projection;
5441
+ projection = RVal.project(collectionName, boundIndex);
5442
+ return Step["let"](asyncVars[i], projection, recurse(i + 1));
5443
+ });
5444
+ })(0);
5445
+ forks = (function() {
5446
+ var _j, _len1, _results;
5447
+ _results = [];
5448
+ for (_j = 0, _len1 = forkBodies.length; _j < _len1; _j++) {
5449
+ _ = forkBodies[_j];
5450
+ _results.push(nameGen('f'));
5451
+ }
5452
+ return _results;
5453
+ })();
5454
+ return Step.letCont(contName, [collectionName], bindAsyncVars, Step.makeVar(RVal.constant(asyncExprs.length), function(order) {
5455
+ return Step.letJoin(joinName, order, contName, (recurse = function(i) {
5456
+ if (i >= forkBodies.length) {
5457
+ return Step.fork(forks);
5458
+ } else {
5459
+ return Step.letCont(forks[i], [], forkBodies[i], recurse(i + 1));
5460
+ }
5461
+ })(0));
5462
+ }));
5463
+ };
5464
+ sequence = function(core, rescue, bind) {
5465
+ return core.cases({
5466
+ variable: function(name) {
5467
+ return bind(name);
5468
+ },
5469
+ constant: function(val) {
5470
+ return Step.makeVar(RVal.constant(val), bind);
5471
+ },
5472
+ global: function() {
5473
+ return Step.makeVar(RVal.global(), bind);
5474
+ },
5475
+ bind: function(varName, valExpr, expr) {
5476
+ return sequence(valExpr, rescue, function(bound) {
5477
+ return sequence(expr.subst(varName, Core.variable(bound)), rescue, bind);
5478
+ });
5479
+ },
5480
+ block: function(argName, body) {
5481
+ return bind(RVal.makeLambda(argName, function(rescue, next) {
5482
+ return sequenceTail(body, rescue, next);
5483
+ }));
5484
+ },
5485
+ app: function(block, arg) {
5486
+ return Step.makeCont(1, bind, function(cont) {
5487
+ return bindExprs([block, arg], rescue, function(_arg) {
5488
+ var boundArg, boundBlock;
5489
+ boundBlock = _arg[0], boundArg = _arg[1];
5490
+ return Step.app(boundBlock, [boundArg], rescue, cont);
5491
+ });
5492
+ });
5493
+ },
5494
+ len: function(expr) {
5495
+ return sequence(expr, rescue, function(boundExpr) {
5496
+ return Step.makeVar(RVal.prim(1, 'length', [boundExpr]), bind);
5497
+ });
5498
+ },
5499
+ op1: function(op, arg) {
5500
+ return sequence(arg, rescue, function(boundArg) {
5501
+ return Step.makeVar(RVal.prim(1, op, [boundArg]), bind);
5502
+ });
5503
+ },
5504
+ op2: function(op, lhs, rhs) {
5505
+ return bindExprs([lhs, rhs], rescue, function(_arg) {
5506
+ var l, r;
5507
+ l = _arg[0], r = _arg[1];
5508
+ return Step.makeVar(RVal.prim(2, op, [l, r]), bind);
5509
+ });
5510
+ },
5511
+ query: function(expr, annotations) {
5512
+ return sequence(expr, rescue, function(boundExpr) {
5513
+ return Step.makeCont(1, bind, function(outCont) {
5514
+ return Step.query(annotations, boundExpr, rescue, outCont);
5515
+ });
5516
+ });
5517
+ },
5518
+ localQuery: function(key) {
5519
+ return Step.makeCont(1, bind, function(outCont) {
5520
+ return Step.localQuery(key, rescue, outCont);
5521
+ });
5522
+ },
5523
+ branch: function(cond, ifTrue, ifFalse) {
5524
+ return sequence(cond, rescue, function(boundCond) {
5525
+ return Step.makeCont(1, bind, function(joinCont) {
5526
+ var falseBody, trueBody;
5527
+ trueBody = sequenceTail(ifTrue, rescue, joinCont);
5528
+ falseBody = sequenceTail(ifFalse, rescue, joinCont);
5529
+ return Step.makeCont(0, (function() {
5530
+ return trueBody;
5531
+ }), function(trueCont) {
5532
+ return Step.makeCont(0, (function() {
5533
+ return falseBody;
5534
+ }), function(falseCont) {
5535
+ return Step["if"](boundCond, trueCont, falseCont);
5536
+ });
5537
+ });
5538
+ });
5539
+ });
5540
+ },
5541
+ fail: function(message) {
5542
+ return Step.makeVar(RVal.constant(message), function(boundMessage) {
5543
+ return Step.next(rescue, [boundMessage]);
5544
+ });
5545
+ },
5546
+ pair: function(first, second) {
5547
+ return bindExprs([first, second], rescue, function(_arg) {
5548
+ var f, s;
5549
+ f = _arg[0], s = _arg[1];
5550
+ return Step.makeVar(RVal.pair(f, s), bind);
5551
+ });
5552
+ },
5553
+ list: function(elements) {
5554
+ return bindExprs(elements, rescue, function(boundEls) {
5555
+ return Step.makeVar(RVal.list(boundEls), bind);
5556
+ });
5557
+ },
5558
+ squishList: function(list) {
5559
+ return list.cases({
5560
+ list: function(elements) {
5561
+ var NULL, e, rescued;
5562
+ NULL = Core.constant(null);
5563
+ rescued = (function() {
5564
+ var _i, _len, _results;
5565
+ _results = [];
5566
+ for (_i = 0, _len = elements.length; _i < _len; _i++) {
5567
+ e = elements[_i];
5568
+ _results.push(e.rescue(NULL));
5569
+ }
5570
+ return _results;
5571
+ })();
5572
+ return bindExprs(rescued, rescue, function(boundExprs) {
5573
+ return Step.makeVar(RVal.list(boundExprs), function(boundList) {
5574
+ return Step.makeVar(RVal.compact(boundList), bind);
5575
+ });
5576
+ });
5577
+ },
5578
+ other: function() {
5579
+ return sequence(list, rescue, function(boundList) {
5580
+ return Step.makeVar(RVal.compact(boundList), bind);
5581
+ });
5582
+ }
5583
+ });
5584
+ },
5585
+ mapList: function(list, arg, idxArg, body) {
5586
+ var joinName;
5587
+ joinName = nameGen('j');
5588
+ return sequence(list, rescue, function(boundList) {
5589
+ return Step.makeVar(RVal.prim(1, 'length', [boundList]), function(boundLen) {
5590
+ return Step.makeCont(1, bind, function(outCont) {
5591
+ return Step.makeJoin(boundLen, outCont, function(index, bindJoin) {
5592
+ var delistExpr, substBody;
5593
+ delistExpr = Core.variable(boundList).delist(Core.variable(index));
5594
+ substBody = body.subst(arg, delistExpr).subst(idxArg, Core.variable(index));
5595
+ return sequence(substBody, rescue, bindJoin);
5596
+ });
5597
+ });
5598
+ });
5599
+ });
5600
+ },
5601
+ filterList: function(list, arg, body) {
5602
+ var joinName, nullName;
5603
+ joinName = nameGen('j');
5604
+ nullName = nameGen('NULL');
5605
+ return sequence(list, rescue, function(boundList) {
5606
+ return Step["let"](nullName, RVal.constant(null), Step.makeVar(RVal.prim(1, 'length', [boundList]), function(boundLen) {
5607
+ var mkCompact;
5608
+ mkCompact = function(v) {
5609
+ return Step.makeVar(RVal.compact(v), bind);
5610
+ };
5611
+ return Step.makeCont(1, mkCompact, function(outCont) {
5612
+ return Step.makeJoin(boundLen, outCont, function(index, bindJoin) {
5613
+ var delistExpr, substBody;
5614
+ delistExpr = Core.variable(boundList).delist(Core.variable(index));
5615
+ substBody = body.subst(arg, delistExpr);
5616
+ return sequence(substBody, rescue, function(mappedBool) {
5617
+ var mkDelete, mkKeep;
5618
+ mkDelete = function() {
5619
+ return bindJoin(nullName);
5620
+ };
5621
+ mkKeep = function() {
5622
+ return sequence(delistExpr, rescue, bindJoin);
5623
+ };
5624
+ return Step.makeCont(0, mkDelete, function(deleteCont) {
5625
+ return Step.makeCont(0, mkKeep, function(keepCont) {
5626
+ return Step["if"](mappedBool, keepCont, deleteCont);
5627
+ });
5628
+ });
5629
+ });
5630
+ });
5631
+ });
5632
+ }));
5633
+ });
5634
+ },
5635
+ delist: function(expr, idxExpr) {
5636
+ return bindExprs([expr, idxExpr], rescue, function(_arg) {
5637
+ var boundExpr, boundIdx, e;
5638
+ boundExpr = _arg[0], boundIdx = _arg[1];
5639
+ e = nameGen('e');
5640
+ return Step["let"](e, RVal.project(boundExpr, boundIdx), bind(e));
5641
+ });
5642
+ },
5643
+ depair: function(expr, key) {
5644
+ return sequence(expr, rescue, function(boundExpr) {
5645
+ return Step.makeVar(RVal.depair(boundExpr, key), bind);
5646
+ });
5647
+ },
5648
+ rescue: function(expr, default_) {
5649
+ return Step.makeCont(1, bind, function(joinCont) {
5650
+ var defaultBody;
5651
+ defaultBody = sequenceTail(default_, rescue, joinCont);
5652
+ return Step.makeCont(0, (function() {
5653
+ return defaultBody;
5654
+ }), function(innerRescue) {
5655
+ return sequenceTail(expr, innerRescue, joinCont);
5656
+ });
5657
+ });
5658
+ },
5659
+ foldList: function(list, out, arg, accumArg, idxArg, body) {
5660
+ var v;
5661
+ v = Core.variable;
5662
+ if (body.isStrictIn(Core.variable(accumArg))) {
5663
+ return sequence(list, rescue, function(boundList) {
5664
+ return sequence(out, rescue, function(boundOut) {
5665
+ var decrIdx, lenExpr, loopName;
5666
+ loopName = nameGen('loop');
5667
+ decrIdx = Core.variable(idxArg).op2('-', Core.constant(1));
5668
+ lenExpr = Core.variable(boundList).len();
5669
+ return sequence(lenExpr, rescue, function(boundLen) {
5670
+ var escapeCond, loopBody;
5671
+ escapeCond = Core.variable(idxArg).op2('===', Core.constant(0));
5672
+ loopBody = sequence(escapeCond, rescue, function(boundEscape) {
5673
+ var continueBody, escapeBody;
5674
+ escapeBody = function() {
5675
+ return bind(accumArg);
5676
+ };
5677
+ continueBody = function() {
5678
+ return sequence(decrIdx, rescue, function(nextIdx) {
5679
+ var substBody;
5680
+ substBody = body.subst(arg, Core.delist(v(boundList), v(nextIdx))).subst(idxArg, Core.variable(nextIdx));
5681
+ return sequence(substBody, rescue, function(nextAccum) {
5682
+ return Step.next(loopName, [nextIdx, nextAccum]);
5683
+ });
5684
+ });
5685
+ };
5686
+ return Step.makeCont(0, escapeBody, function(escapeCont) {
5687
+ return Step.makeCont(0, continueBody, function(continueCont) {
5688
+ return Step["if"](boundEscape, escapeCont, continueCont);
5689
+ });
5690
+ });
5691
+ });
5692
+ return Step.letCont(loopName, [idxArg, accumArg], loopBody, Step.next(loopName, [boundLen, boundOut]));
5693
+ });
5694
+ });
5695
+ });
5696
+ } else if (!body.containsInNonTailPosition(Core.variable(accumArg))) {
5697
+ return sequence(list, rescue, function(boundList) {
5698
+ var lenExpr;
5699
+ lenExpr = Core.variable(boundList).len();
5700
+ return sequence(lenExpr, rescue, function(boundLen) {
5701
+ return Step.makeCont(1, bind, function(outCont) {
5702
+ var escapeBody, loopBody, loopName, processBody, testExpr;
5703
+ loopName = nameGen('l');
5704
+ escapeBody = function() {
5705
+ return sequenceTail(out, rescue, outCont);
5706
+ };
5707
+ processBody = function() {
5708
+ return sequence(v(idxArg).op2('+', Core.constant(1)), rescue, function(incr) {
5709
+ var substBody;
5710
+ substBody = body.subst(arg, Core.delist(v(boundList), v(idxArg))).subst(accumArg, Core.next(loopName, [incr]));
5711
+ return sequenceTail(substBody, rescue, outCont);
5712
+ });
5713
+ };
5714
+ testExpr = v(idxArg).op2('<', v(boundLen));
5715
+ loopBody = Step.makeCont(0, escapeBody, function(escape) {
5716
+ return Step.makeCont(0, processBody, function(process) {
5717
+ return sequence(testExpr, rescue, function(test) {
5718
+ return Step["if"](test, process, escape);
5719
+ });
5720
+ });
5721
+ });
5722
+ return Step.letCont(loopName, [idxArg], loopBody, Step.makeVar(RVal.constant(0), function(zero) {
5723
+ return Step.next(loopName, [zero]);
5724
+ }));
5725
+ });
5726
+ });
5727
+ });
5728
+ } else {
5729
+ debugger;
5730
+ body.isStrictIn(accumArg);
5731
+ throw 'TODO';
5732
+ }
5733
+ }
5734
+ });
5735
+ };
5736
+ sequenceTail = function(core, rescue, next) {
5737
+ return core.cases({
5738
+ next: function(cont, args) {
5739
+ return Step.next(cont, args);
5740
+ },
5741
+ other: function() {
5742
+ return sequence(core, rescue, function(bound) {
5743
+ return Step.next(next, [bound]);
5744
+ });
5745
+ }
5746
+ });
5747
+ };
5748
+ return function(core) {
5749
+ return sequenceTail(core, 'FAIL', 'RETURN');
5750
+ };
5751
+ })();
5752
+
5753
+ Gibbon.reduce = (function() {
5754
+ var betaReduce, reduceWithTrace;
5755
+ betaReduce = function(name, params, body, expr) {
5756
+ return expr.cases({
5757
+ next: function(contName, args) {
5758
+ if (name === contName) {
5759
+
5760
+ return body.substAll(params, args);
5761
+ } else {
5762
+ return this;
5763
+ }
5764
+ },
5765
+ other: function() {
5766
+ return this.map(function(x) {
5767
+ return betaReduce(name, params, body, x);
5768
+ });
5769
+ }
5770
+ });
5771
+ };
5772
+ reduceWithTrace = function(step, trace) {
5773
+ return step.cases({
5774
+ "let": function(lval, value, expr) {
5775
+ var checkDup, goAbort, goConst, goSubst;
5776
+ checkDup = function(val) {
5777
+ var dupVar;
5778
+ dupVar = trace.findVarTrace(value);
5779
+ if (dupVar) {
5780
+
5781
+ return reduceWithTrace(expr.subst(lval, dupVar), trace);
5782
+ }
5783
+ };
5784
+ goSubst = function(varName) {
5785
+ return reduceWithTrace(expr.subst(lval, varName), trace);
5786
+ };
5787
+ goConst = function(c) {
5788
+ var constVal, dup, newTrace;
5789
+ constVal = RVal.constant(c);
5790
+ if ((dup = checkDup(constVal))) {
5791
+ return dup;
5792
+ }
5793
+ newTrace = trace.traceVar(lval, VarTrace.value(constVal));
5794
+ return Step["let"](lval, constVal, reduceWithTrace(expr, newTrace));
5795
+ };
5796
+ goAbort = function() {
5797
+ var dup, reduced;
5798
+ if ((dup = checkDup(value))) {
5799
+ return dup;
5800
+ }
5801
+ reduced = reduceWithTrace(expr, trace.extendWith(step));
5802
+ if (reduced.hasFree(lval)) {
5803
+ return Step["let"](lval, value, reduced);
5804
+ } else {
5805
+
5806
+ return reduced;
5807
+ }
5808
+ };
5809
+ return value.cases({
5810
+ prim: function(arity, op, args) {
5811
+ var checkIdent, constFold, identFold, left, leftTrace, leftVal, right, rightTrace, rightVal, vTrace;
5812
+ if (arity === 1) {
5813
+ vTrace = trace.getVar(args[0]);
5814
+ if (vTrace._tag === 'value' && vTrace.val._tag === 'constant') {
5815
+ return goConst(applyOp1(op, vTrace.val.value));
5816
+ } else {
5817
+ return goAbort();
5818
+ }
5819
+ } else if (arity === 2) {
5820
+ left = args[0], right = args[1];
5821
+ leftTrace = trace.getVar(left);
5822
+ rightTrace = trace.getVar(right);
5823
+ leftVal = leftTrace._tag === 'value' && leftTrace.val;
5824
+ rightVal = rightTrace._tag === 'value' && rightTrace.val;
5825
+ checkIdent = function(opTest, val, ident, identVal) {
5826
+ return op === opTest && val && val._tag === 'constant' && val.value === ident && identVal();
5827
+ };
5828
+ identFold = checkIdent('*', leftVal, 0, function() {
5829
+ return goConst(0);
5830
+ }) || checkIdent('*', rightVal, 0, function() {
5831
+ return goConst(0);
5832
+ }) || checkIdent('*', leftVal, 1, function() {
5833
+ return goSubst(right);
5834
+ }) || checkIdent('*', rightVal, 1, function() {
5835
+ return goSubst(left);
5836
+ }) || checkIdent('+', leftVal, 0, function() {
5837
+ return goSubst(right);
5838
+ }) || checkIdent('+', rightVal, 0, function() {
5839
+ return goSubst(left);
5840
+ }) || checkIdent('/', rightVal, 1, function() {
5841
+ return goSubst(left);
5842
+ });
5843
+ if (identFold) {
5844
+
5845
+ return identFold;
5846
+ }
5847
+ if (leftVal && leftVal._tag === 'constant' && rightVal && rightVal._tag === 'constant') {
5848
+ constFold = applyOp2(op, leftVal.value, rightVal.value);
5849
+
5850
+ return goConst(constFold);
5851
+ }
5852
+ return goAbort();
5853
+ }
5854
+ },
5855
+ other: function() {
5856
+ return goAbort();
5857
+ }
5858
+ });
5859
+ },
5860
+ letCont: function(contName, argNames, body, expr) {
5861
+ var extended, reducedBody, tryBeta;
5862
+ extended = trace.extendWith(this);
5863
+ tryBeta = function() {
5864
+ var betaReduced, reduced;
5865
+ if (reducedBody.hasFree(contName)) {
5866
+
5867
+ betaReduced = expr;
5868
+ } else {
5869
+ betaReduced = betaReduce(contName, argNames, reducedBody, expr);
5870
+ }
5871
+ reduced = reduceWithTrace(betaReduced, extended);
5872
+ if (!reduced.hasFree(contName)) {
5873
+
5874
+ return reduced;
5875
+ }
5876
+ return Step.letCont(contName, argNames, reducedBody, reduced);
5877
+ };
5878
+ reducedBody = reduceWithTrace(body, extended);
5879
+ return reducedBody.cases({
5880
+ next: function(innerName, innerArgs) {
5881
+ if (equalArrays(innerArgs, argNames)) {
5882
+
5883
+ return reduceWithTrace(expr.subst(contName, innerName), trace);
5884
+ } else {
5885
+ return tryBeta();
5886
+ }
5887
+ },
5888
+ other: function() {
5889
+ return tryBeta();
5890
+ }
5891
+ });
5892
+ },
5893
+ other: function() {
5894
+ var _this = this;
5895
+ return this.map(function(x) {
5896
+ return reduceWithTrace(x, trace.extendWith(_this));
5897
+ });
5898
+ }
5899
+ });
5900
+ };
5901
+ return function(step) {
5902
+ var reduced;
5903
+
5904
+
5905
+
5906
+ reduced = reduceWithTrace(step, Trace.empty());
5907
+
5908
+
5909
+ return reduced;
5910
+ };
5911
+ })();
5912
+
5913
+ Gibbon.JS = JS = (function(_super) {
5914
+ var inspectString, oldBlock, validIdent;
5915
+
5916
+ __extends(JS, _super);
5917
+
5918
+ function JS() {
5919
+ _ref19 = JS.__super__.constructor.apply(this, arguments);
5920
+ return _ref19;
5921
+ }
5922
+
5923
+ JS.variants({
5924
+ func: ['name', 'args', 'block'],
5925
+ json: ['obj'],
5926
+ block: ['statements'],
5927
+ ident: ['name'],
5928
+ funcall: ['callee', 'args'],
5929
+ literal: ['value'],
5930
+ array: ['elements'],
5931
+ object: ['keys', 'vals'],
5932
+ access: ['expr', 'name'],
5933
+ bind: ['lhs', 'rhs'],
5934
+ ternary: ['cond', 'ifTrue', 'ifFalse'],
5935
+ "if": ['cond', 'ifTrue', 'ifFalse'],
5936
+ "return": ['expr'],
5937
+ operator: ['operator', 'lhs', 'rhs'],
5938
+ forLoop: ['len', 'arg', 'body'],
5939
+ whileLoop: ['cond', 'body'],
5940
+ varDecl: ['name']
5941
+ });
5942
+
5943
+ oldBlock = JS.block;
5944
+
5945
+ JS.block = function(s) {
5946
+ if (!isArray(s)) {
5947
+ throw 'lol';
5948
+ }
5949
+ return oldBlock(s);
5950
+ };
5951
+
5952
+ JS.prototype.toFunction = function() {
5953
+ return this.cases({
5954
+ func: function(name, args, block) {
5955
+ return (function(func, args, ctor) {
5956
+ ctor.prototype = func.prototype;
5957
+ var child = new ctor, result = func.apply(child, args);
5958
+ return Object(result) === result ? result : child;
5959
+ })(Function, __slice.call(args).concat([block.toJS()]), function(){});
5960
+ },
5961
+ other: function() {
5962
+ return JS.func('tmp', [], this).toFunction();
5963
+ }
5964
+ });
5965
+ };
5966
+
5967
+ JS.trap = function(cond) {
5968
+ return JS["if"](cond, JS["return"](null), null);
5969
+ };
5970
+
5971
+ JS.iife = function(statements) {
5972
+ return JS.funcall(JS.func(null, [], JS.block(statements)), []);
5973
+ };
5974
+
5975
+ JS.tailcall = function(fn, args) {
5976
+ return JS["return"](JS.funcall(fn, args));
5977
+ };
5978
+
5979
+ JS.prototype.op = function(o, other) {
5980
+ return JS.operator(o, this, other);
5981
+ };
5982
+
5983
+ JS.prototype.eq = function(other) {
5984
+ return this.op('===', other);
5985
+ };
5986
+
5987
+ JS.prototype.lt = function(other) {
5988
+ return this.op('<', other);
5989
+ };
5990
+
5991
+ JS.prototype.funcall = function(args) {
5992
+ return JS.funcall(this, args);
5993
+ };
5994
+
5995
+ JS.prototype.tailcall = function(args) {
5996
+ return JS.tailcall(this, args);
5997
+ };
5998
+
5999
+ JS.prototype.methodcall = function(name, args) {
6000
+ return this.access(JS.literal(name)).funcall(args);
6001
+ };
6002
+
6003
+ JS.prototype.access = function(key) {
6004
+ return JS.access(this, key);
6005
+ };
6006
+
6007
+ JS.trampoline = function(varName) {
6008
+ var cond, v;
6009
+ v = JS.ident(varName);
6010
+ cond = JS.ident('typeof').funcall([v]).op('===', JS.literal('function'));
6011
+ return JS.whileLoop(cond, JS.bind(v, v.funcall([])));
6012
+ };
6013
+
6014
+ inspectString = function(str) {
6015
+ var escaped;
6016
+ escaped = str.replace("\\", "\\\\").replace('"', '\\"').replace("\n", "\\n");
6017
+ return "\"" + escaped + "\"";
6018
+ };
6019
+
6020
+ JS.prototype.inspect = function() {
6021
+ return "[JS " + (this.toJS()) + "]";
6022
+ };
6023
+
6024
+ validIdent = /^[\w$][\w\d$]*$/;
6025
+
6026
+ JS.prototype.toJS = function(indent, block) {
6027
+ var I, i;
6028
+ if (indent == null) {
6029
+ indent = 0;
6030
+ }
6031
+ i = Array(indent + 1).join(' ');
6032
+ I = block ? i : '';
6033
+ return this.cases({
6034
+ func: function(name, args, block) {
6035
+ var header;
6036
+ header = name ? "function " + name : "function";
6037
+ return "" + I + header + "(" + (args.join(', ')) + ") " + (block.toJS(indent));
6038
+ },
6039
+ json: function(obj) {
6040
+ return JSON.stringify(obj);
6041
+ },
6042
+ ident: function(name) {
6043
+ return name;
6044
+ },
6045
+ array: function(els) {
6046
+ var e;
6047
+ return "" + I + "[" + (((function() {
6048
+ var _i, _len, _results;
6049
+ _results = [];
6050
+ for (_i = 0, _len = els.length; _i < _len; _i++) {
6051
+ e = els[_i];
6052
+ _results.push(e.toJS(indent));
6053
+ }
6054
+ return _results;
6055
+ })()).join(', ')) + "]";
6056
+ },
6057
+ object: function(keys, vals) {
6058
+ var k, pairs;
6059
+ pairs = (function() {
6060
+ var _i, _len, _results;
6061
+ _results = [];
6062
+ for (i = _i = 0, _len = keys.length; _i < _len; i = ++_i) {
6063
+ k = keys[i];
6064
+ _results.push("" + (inspectString(k)) + ": " + (vals[i].toJS()));
6065
+ }
6066
+ return _results;
6067
+ })();
6068
+ return "" + I + "{ " + (pairs.join(', ')) + " }";
6069
+ },
6070
+ funcall: function(callee, args) {
6071
+ var a;
6072
+ args = ((function() {
6073
+ var _i, _len, _results;
6074
+ _results = [];
6075
+ for (_i = 0, _len = args.length; _i < _len; _i++) {
6076
+ a = args[_i];
6077
+ _results.push(a.toJS(indent));
6078
+ }
6079
+ return _results;
6080
+ })()).join(', ');
6081
+ return "" + I + (callee.toJS(indent)) + "(" + args + ")";
6082
+ },
6083
+ literal: function(value) {
6084
+ if (typeof value === 'string') {
6085
+ return inspectString(value);
6086
+ }
6087
+ return '' + value;
6088
+ },
6089
+ "if": function(cond, ifTrue, ifFalse) {
6090
+ var elseBranch;
6091
+ elseBranch = "";
6092
+ if (ifFalse) {
6093
+ elseBranch = "\n" + i + "else " + (ifFalse.toJS(indent));
6094
+ }
6095
+ return "" + I + "if (" + (cond.toJS(indent, true)) + ") " + (ifTrue.toJS(indent)) + elseBranch;
6096
+ },
6097
+ block: function(statements) {
6098
+ var s;
6099
+ return "" + I + "{\n" + (((function() {
6100
+ var _i, _len, _results;
6101
+ _results = [];
6102
+ for (_i = 0, _len = statements.length; _i < _len; _i++) {
6103
+ s = statements[_i];
6104
+ _results.push(s.toJS(indent + 1, true) + ';');
6105
+ }
6106
+ return _results;
6107
+ })()).join('\n')) + "\n" + i + "}";
6108
+ },
6109
+ forLoop: function(len, arg, body) {
6110
+ arg = arg.toJS(indent);
6111
+ len = len.toJS(indent);
6112
+ body = body.toJS(indent);
6113
+ return "" + i + "for (var " + arg + "=0; " + arg + "<" + len + "; " + arg + "+=1) " + body;
6114
+ },
6115
+ whileLoop: function(cond, body) {
6116
+ return "" + I + "while (" + (cond.toJS(indent)) + ") " + (body.toJS(indent));
6117
+ },
6118
+ varDecl: function(name) {
6119
+ return "" + i + "var " + name;
6120
+ },
6121
+ access: function(expr, name) {
6122
+ var access;
6123
+ access = name._tag === 'literal' && typeof name.value === 'string' && validIdent.test(name.value) ? "." + name.value : "[" + (name.toJS(indent)) + "]";
6124
+ return "" + (expr.toJS(indent)) + access;
6125
+ },
6126
+ bind: function(lhs, rhs) {
6127
+ return "" + i + (lhs.toJS(indent)) + " = " + (rhs.toJS(indent));
6128
+ },
6129
+ "return": function(expr) {
6130
+ if (expr) {
6131
+ return "" + I + "return " + (expr.toJS(indent));
6132
+ } else {
6133
+ return "" + I + "return";
6134
+ }
6135
+ },
6136
+ operator: function(op, lhs, rhs) {
6137
+ return "(" + (lhs.toJS(indent)) + ")" + op + "(" + (rhs.toJS(indent)) + ")";
6138
+ }
6139
+ });
6140
+ };
6141
+
6142
+ return JS;
6143
+
6144
+ })(Variant);
6145
+
6146
+ Gibbon.codegen = (function() {
6147
+ var generate, inlinePrim;
6148
+ inlinePrim = {
6149
+ 2: {
6150
+ '+': function(_arg) {
6151
+ var l, r;
6152
+ l = _arg[0], r = _arg[1];
6153
+ return l.op('+', r);
6154
+ },
6155
+ '*': function(_arg) {
6156
+ var l, r;
6157
+ l = _arg[0], r = _arg[1];
6158
+ return l.op('*', r);
6159
+ },
6160
+ '-': function(_arg) {
6161
+ var l, r;
6162
+ l = _arg[0], r = _arg[1];
6163
+ return l.op('-', r);
6164
+ },
6165
+ '/': function(_arg) {
6166
+ var l, r;
6167
+ l = _arg[0], r = _arg[1];
6168
+ return l.op('/', r);
6169
+ },
6170
+ '<': function(_arg) {
6171
+ var l, r;
6172
+ l = _arg[0], r = _arg[1];
6173
+ return l.op('<', r);
6174
+ },
6175
+ '<=': function(_arg) {
6176
+ var l, r;
6177
+ l = _arg[0], r = _arg[1];
6178
+ return l.op('<=', r);
6179
+ },
6180
+ '>': function(_arg) {
6181
+ var l, r;
6182
+ l = _arg[0], r = _arg[1];
6183
+ return l.op('>', r);
6184
+ },
6185
+ '>=': function(_arg) {
6186
+ var l, r;
6187
+ l = _arg[0], r = _arg[1];
6188
+ return l.op('>=', r);
6189
+ },
6190
+ '===': function(_arg) {
6191
+ var l, r;
6192
+ l = _arg[0], r = _arg[1];
6193
+ return l.op('===', r);
6194
+ }
6195
+ },
6196
+ 1: {
6197
+ length: function(_arg) {
6198
+ var l;
6199
+ l = _arg[0];
6200
+ return l.access(JS.literal('length'));
6201
+ },
6202
+ '!': function(_arg) {
6203
+ var a;
6204
+ a = _arg[0];
6205
+ return JS.funcall(JS.ident('!'), [a]);
6206
+ },
6207
+ '-': function(_arg) {
6208
+ var a;
6209
+ a = _arg[0];
6210
+ return JS.funcall(JS.ident('-'), [a]);
6211
+ }
6212
+ }
6213
+ };
6214
+ generate = function(term, trace, push) {
6215
+ var extended, varToJS;
6216
+ varToJS = function(varName) {
6217
+ return trace.getVar(varName).cases({
6218
+ continued: function() {
6219
+ return JS.ident(varName);
6220
+ },
6221
+ value: function(val) {
6222
+ return val.cases({
6223
+ constant: function(v) {
6224
+ return JS.literal(v);
6225
+ },
6226
+ global: function() {
6227
+ return JS.ident('$');
6228
+ },
6229
+ lambda: function() {
6230
+ throw 'TODO';
6231
+ },
6232
+ prim: function(arity, name, args) {
6233
+ var a, jsArgs;
6234
+ jsArgs = (function() {
6235
+ var _i, _len, _results;
6236
+ _results = [];
6237
+ for (_i = 0, _len = args.length; _i < _len; _i++) {
6238
+ a = args[_i];
6239
+ _results.push(varToJS(a));
6240
+ }
6241
+ return _results;
6242
+ })();
6243
+ return inlinePrim[arity][name](jsArgs);
6244
+ },
6245
+ list: function(els) {
6246
+ var e;
6247
+ return JS.array((function() {
6248
+ var _i, _len, _results;
6249
+ _results = [];
6250
+ for (_i = 0, _len = els.length; _i < _len; _i++) {
6251
+ e = els[_i];
6252
+ _results.push(varToJS(e));
6253
+ }
6254
+ return _results;
6255
+ })());
6256
+ },
6257
+ pair: function(first, second) {
6258
+ return JS.object(['first', 'second'], [varToJS(first), varToJS(second)]);
6259
+ },
6260
+ depair: function(val, key) {
6261
+ return varToJS(val).access(JS.literal(key));
6262
+ },
6263
+ project: function(val, index) {
6264
+ return varToJS(val).access(varToJS(index));
6265
+ },
6266
+ compact: function() {
6267
+ return JS.ident(varName);
6268
+ }
6269
+ });
6270
+ }
6271
+ });
6272
+ };
6273
+ extended = trace.extendWith(term);
6274
+ return term.cases({
6275
+ "let": function(name, val, expr) {
6276
+ var _compact, _i, _len, _push;
6277
+ if (val._tag === 'compact') {
6278
+ val = varToJS(val.val);
6279
+ _i = JS.ident('_i');
6280
+ _compact = JS.ident('_compact');
6281
+ _len = JS.ident('_len');
6282
+ _push = JS.access(_compact, JS.literal('push'));
6283
+ push(JS.varDecl(name));
6284
+ push(JS.bind(JS.ident(name), JS.iife([JS.varDecl('_compact'), JS.varDecl('_len'), JS.bind(_len, JS.access(val, JS.literal('length'))), JS.bind(_compact, JS.array([])), JS.forLoop(_len, _i, JS.block([JS["if"](JS.access(val, _i).op('!=', JS.literal(null)), JS.funcall(_push, [JS.access(val, _i)]), null)])), JS["return"](_compact)])));
6285
+ }
6286
+ return generate(expr, extended, push);
6287
+ },
6288
+ letCont: function(name, args, body, expr) {
6289
+ var bodyStatements;
6290
+ bodyStatements = [];
6291
+ generate(body, extended, function(s) {
6292
+ return bodyStatements.push(s);
6293
+ });
6294
+ push(JS.func(name, args, JS.block(bodyStatements)));
6295
+ return generate(expr, extended, push);
6296
+ },
6297
+ letJoin: function(name, order, cont, expr) {
6298
+ var counterName, resultsName;
6299
+ counterName = "" + name + "$counter";
6300
+ resultsName = "" + name + "$results";
6301
+ push(JS.varDecl(counterName));
6302
+ push(JS.varDecl(resultsName));
6303
+ push(JS.bind(JS.ident(counterName), varToJS(order)));
6304
+ push(JS.bind(JS.ident(resultsName), JS.array([])));
6305
+ push(JS.func(name, ['idx', 'el'], JS.block([JS.bind(JS.access(JS.ident(resultsName), JS.ident('idx')), JS.ident('el')), JS.trap(JS.ident(counterName).op('-=', JS.literal(1)).op('>', JS.literal(0))), JS.ident(cont).tailcall([JS.ident(resultsName)])])));
6306
+ return generate(expr, extended, push);
6307
+ },
6308
+ fork: function(forks) {
6309
+ var cont, head, last, thunk, _i, _j, _len;
6310
+ head = 2 <= forks.length ? __slice.call(forks, 0, _i = forks.length - 1) : (_i = 0, []), last = forks[_i++];
6311
+ thunk = nameGen('_thunk');
6312
+ push(JS.varDecl(thunk));
6313
+ for (_j = 0, _len = head.length; _j < _len; _j++) {
6314
+ cont = head[_j];
6315
+ push(JS.bind(JS.ident(thunk), JS.ident(cont).funcall([])));
6316
+ push(JS.trampoline(thunk));
6317
+ }
6318
+ return push(JS.ident(last).tailcall([]));
6319
+ },
6320
+ each: function(length, cont) {
6321
+ var minus1, thunk, _i;
6322
+ _i = JS.ident('_i');
6323
+ thunk = nameGen('_thunk');
6324
+ push(JS.varDecl(thunk));
6325
+ minus1 = varToJS(length).op('+', JS.literal(-1));
6326
+ push(JS.forLoop(minus1, _i, JS.block([JS.bind(JS.ident(thunk), JS.ident(cont).funcall([_i])), JS.trampoline(thunk)])));
6327
+ return push(JS.ident(cont).tailcall([minus1]));
6328
+ },
6329
+ next: function(cont, args) {
6330
+ var a;
6331
+ return push(JS.ident(cont).tailcall((function() {
6332
+ var _i, _len, _results;
6333
+ _results = [];
6334
+ for (_i = 0, _len = args.length; _i < _len; _i++) {
6335
+ a = args[_i];
6336
+ _results.push(varToJS(a));
6337
+ }
6338
+ return _results;
6339
+ })()));
6340
+ },
6341
+ app: function(fn, args, rescue, next) {
6342
+ var a;
6343
+ args = (function() {
6344
+ var _i, _len, _results;
6345
+ _results = [];
6346
+ for (_i = 0, _len = args.length; _i < _len; _i++) {
6347
+ a = args[_i];
6348
+ _results.push(varToJS(a));
6349
+ }
6350
+ return _results;
6351
+ })();
6352
+ return push(varToJS(fn).tailcall(__slice.call(args).concat([JS.ident(rescue)], [JS.ident(next)])));
6353
+ },
6354
+ query: function(annotations, arg, rescue, next) {
6355
+ return push(JS.ident('QUERY').tailcall([varToJS(arg), JS.json(annotations), JS.ident(rescue), JS.ident(next)]));
6356
+ },
6357
+ localQuery: function(key, rescue, next) {
6358
+ return push(JS.ident('QUERY').tailcall([JS.literal(null), JS.literal(key), JS.ident(rescue), JS.ident(next)]));
6359
+ },
6360
+ "if": function(cond, trueCont, falseCont) {
6361
+ return push(JS["if"](varToJS(cond), JS.ident(trueCont).tailcall([]), JS.ident(falseCont).tailcall([])));
6362
+ }
6363
+ });
6364
+ };
6365
+ return function(term) {
6366
+ var out, statements;
6367
+ statements = [];
6368
+ generate(term, Trace.empty(), function(s) {
6369
+ return statements.push(s);
6370
+ });
6371
+ out = JS.func('compiled', ['$', 'QUERY', 'FAIL', 'RETURN'], JS.block(statements));
6372
+
6373
+
6374
+ return out;
6375
+ };
6376
+ })();
6377
+
6378
+ stdlib = Gibbon.stdlib = (function() {
6379
+ var FALSE, TRUE, equals;
6380
+ TRUE = Core.constant(true);
6381
+ FALSE = Core.constant(false);
6382
+ equals = function(x, y, type) {
6383
+ var direct;
6384
+ direct = function() {
6385
+ return x.op2('===', y);
6386
+ };
6387
+ return type.cases({
6388
+ entity: direct,
6389
+ numeric: direct,
6390
+ string: direct,
6391
+ bool: direct,
6392
+ block: function() {
6393
+ return FALSE;
6394
+ },
6395
+ pair: function() {
6396
+ var eq1, eq2;
6397
+ eq1 = equals(x.depair('first'), y.depair('first'), type.first);
6398
+ eq2 = equals(x.depair('first'), y.depair('second'), type.second);
6399
+ return eq1.branch(eq2, FALSE);
6400
+ },
6401
+ list: function() {
6402
+ var eqEls, eqLength;
6403
+ eqLength = equals(x.len(), y.len(), Type.numeric());
6404
+ eqEls = x.zipList(y).foldList(TRUE, function(pair, next) {
6405
+ var eq;
6406
+ eq = equals(pair.depair('first'), pair.depair('second'), type.of);
6407
+ return next.branch(FALSE, eq);
6408
+ });
6409
+ return eqLength.branch(eqEls, FALSE);
6410
+ }
6411
+ });
6412
+ };
6413
+ return {
6414
+ "case": {
6415
+ type: parse.type('case [bool : %b] = % -> %b'),
6416
+ compile: function(_, _arg) {
6417
+ var alts;
6418
+ alts = _arg[0];
6419
+ return alts.foldList(Core.fail('non-exhaustive cases'), function(el, next) {
6420
+ return el.depair('first').branch(el.depair('second'), next);
6421
+ });
6422
+ }
3965
6423
  },
3966
6424
  "case-eq": {
3967
6425
  type: parse.type('case-eq [%a : %b] = %a -> %b'),
@@ -3969,11 +6427,11 @@ stdlib = Gibbon.stdlib = (function() {
3969
6427
  var alts, eqType;
3970
6428
  alts = _arg[0];
3971
6429
  eqType = tvars.get('a');
3972
- return alts.foldList(IR.fail('non-exhaustive cases'), function(el, next) {
6430
+ return alts.foldList(Core.fail('non-exhaustive cases'), function(el, next) {
3973
6431
  var first, second;
3974
6432
  first = el.depair('first');
3975
6433
  second = el.depair('second');
3976
- return compEquals(input, first, eqType).branch(second, next);
6434
+ return equals(input, first, eqType).branch(second, next);
3977
6435
  });
3978
6436
  }
3979
6437
  },
@@ -3987,23 +6445,23 @@ stdlib = Gibbon.stdlib = (function() {
3987
6445
  var first, second;
3988
6446
  first = el.depair('first');
3989
6447
  second = el.depair('second');
3990
- return compEquals(input, first, eqType).branch(second, next);
6448
+ return equals(input, first, eqType).branch(second, next);
3991
6449
  });
3992
6450
  }
3993
6451
  },
3994
6452
  "any-true?": {
3995
6453
  type: parse.type('any-true? = [bool] -> bool'),
3996
6454
  compile: function(list) {
3997
- return list.foldList(iFalse, function(el, next) {
3998
- return el.branch(iTrue, next);
6455
+ return list.foldList(FALSE, function(el, next) {
6456
+ return el.branch(TRUE, next);
3999
6457
  });
4000
6458
  }
4001
6459
  },
4002
6460
  "all-true?": {
4003
6461
  type: parse.type('all-true? = [bool] -> bool'),
4004
6462
  compile: function(list) {
4005
- return list.foldList(iTrue, function(el, next) {
4006
- return el.branch(next, iFalse);
6463
+ return list.foldList(TRUE, function(el, next) {
6464
+ return el.branch(next, FALSE);
4007
6465
  });
4008
6466
  }
4009
6467
  },
@@ -4012,8 +6470,8 @@ stdlib = Gibbon.stdlib = (function() {
4012
6470
  compile: function(list, _arg) {
4013
6471
  var block;
4014
6472
  block = _arg[0];
4015
- return list.foldList(iFalse, function(el, next) {
4016
- return block.app(el).branch(iTrue, next);
6473
+ return list.foldList(FALSE, function(el, next) {
6474
+ return block.app(el).branch(TRUE, next);
4017
6475
  });
4018
6476
  }
4019
6477
  },
@@ -4022,8 +6480,8 @@ stdlib = Gibbon.stdlib = (function() {
4022
6480
  compile: function(list, _arg) {
4023
6481
  var block;
4024
6482
  block = _arg[0];
4025
- return list.foldList(iTrue, function(el, next) {
4026
- return block.app(el).branch(next, iFalse);
6483
+ return list.foldList(TRUE, function(el, next) {
6484
+ return block.app(el).branch(next, FALSE);
4027
6485
  });
4028
6486
  }
4029
6487
  },
@@ -4033,17 +6491,15 @@ stdlib = Gibbon.stdlib = (function() {
4033
6491
  var elType, needle;
4034
6492
  needle = _arg[0];
4035
6493
  elType = tvars.get('a');
4036
- return list.foldList(iFalse, function(el, next) {
4037
- return compEquals(el, needle, elType).branch(iTrue, next);
6494
+ return list.foldList(FALSE, function(el, next) {
6495
+ return next.branch(TRUE, equals(el, needle, elType));
4038
6496
  });
4039
6497
  }
4040
6498
  },
4041
6499
  "empty?": {
4042
6500
  type: parse.type('empty? = [%] -> bool'),
4043
6501
  compile: function(list) {
4044
- return list.foldList(iTrue, function() {
4045
- return iFalse;
4046
- });
6502
+ return list.len().op2('===', Core.constant(0));
4047
6503
  }
4048
6504
  },
4049
6505
  weight: {
@@ -4051,16 +6507,16 @@ stdlib = Gibbon.stdlib = (function() {
4051
6507
  compile: function(_, _arg) {
4052
6508
  var ratio, weights;
4053
6509
  weights = _arg[0];
4054
- ratio = weights.foldList(IR.pair(IR.constant(0), IR.constant(0)), function(el, next) {
6510
+ ratio = weights.foldList(Core.pair(Core.constant(0), Core.constant(0)), function(el, next) {
4055
6511
  var denominator, numerator, value, weight, weighted;
4056
6512
  value = el.depair('first');
4057
6513
  weight = el.depair('second');
4058
6514
  numerator = next.depair('first');
4059
6515
  denominator = next.depair('second');
4060
- weighted = value.binop('*', weight);
4061
- return IR.pair(numerator.binop('+', weighted), denominator.binop('+', weight));
6516
+ weighted = value.op2('*', weight);
6517
+ return Core.pair(numerator.op2('+', weighted), denominator.op2('+', weight));
4062
6518
  });
4063
- return ratio.depair('first').binop('/', ratio.depair('second'));
6519
+ return ratio.depair('first').op2('/', ratio.depair('second'));
4064
6520
  }
4065
6521
  },
4066
6522
  filter: {
@@ -4082,13 +6538,13 @@ stdlib = Gibbon.stdlib = (function() {
4082
6538
  domHigh = dom.depair('second');
4083
6539
  rangeLow = range.depair('first');
4084
6540
  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);
6541
+ input = input.op2('<', domLow).branch(domLow, input);
6542
+ input = input.op2('>', domHigh).branch(domHigh, input);
6543
+ domSize = domHigh.op2('+', domLow.op1('-'));
6544
+ rangeSize = rangeHigh.op2('+', rangeLow.op1('-'));
6545
+ translated = input.op2('+', domLow.op1('-'));
6546
+ scaled = translated.op2('*', rangeSize.op2('/', domSize));
6547
+ retranslated = scaled.op2('+', rangeLow);
4092
6548
  return retranslated;
4093
6549
  }
4094
6550
  },
@@ -4111,8 +6567,8 @@ stdlib = Gibbon.stdlib = (function() {
4111
6567
  sum: {
4112
6568
  type: parse.type('sum = [numeric] -> numeric'),
4113
6569
  compile: function(list) {
4114
- return list.foldList(IR.constant(0), function(el, next) {
4115
- return el.binop('+', next);
6570
+ return list.foldList(Core.constant(0), function(el, next) {
6571
+ return el.op2('+', next);
4116
6572
  });
4117
6573
  }
4118
6574
  },
@@ -4121,24 +6577,18 @@ stdlib = Gibbon.stdlib = (function() {
4121
6577
  compile: function(_, _arg) {
4122
6578
  var list;
4123
6579
  list = _arg[0];
4124
- return list.foldList(IR.constant(0), function(el, next) {
6580
+ return list.foldList(Core.constant(0), function(el, next) {
4125
6581
  var cond, val;
4126
6582
  cond = el.depair('first');
4127
6583
  val = el.depair('second');
4128
- return cond.branch(val.binop('+', next), next);
6584
+ return cond.branch(val.op2('+', next), next);
4129
6585
  });
4130
6586
  }
4131
6587
  },
4132
6588
  first: {
4133
6589
  type: parse.type('first = [%a] -> %a'),
4134
6590
  compile: function(list) {
4135
- return list.delist(IR.constant(0));
4136
- }
4137
- },
4138
- squish: {
4139
- type: parse.type('squish = [%a] -> [%a]'),
4140
- compile: function(list) {
4141
- return list.squishList();
6591
+ return list.delist(Core.constant(0));
4142
6592
  }
4143
6593
  },
4144
6594
  add: {
@@ -4146,7 +6596,7 @@ stdlib = Gibbon.stdlib = (function() {
4146
6596
  compile: function(input, _arg) {
4147
6597
  var num;
4148
6598
  num = _arg[0];
4149
- return input.binop('+', num);
6599
+ return input.op2('+', num);
4150
6600
  }
4151
6601
  },
4152
6602
  sub: {
@@ -4154,7 +6604,7 @@ stdlib = Gibbon.stdlib = (function() {
4154
6604
  compile: function(input, _arg) {
4155
6605
  var num;
4156
6606
  num = _arg[0];
4157
- return input.binop('+', num.extern('-'));
6607
+ return input.op2('+', num.op1('-'));
4158
6608
  }
4159
6609
  },
4160
6610
  id: {
@@ -4166,13 +6616,13 @@ stdlib = Gibbon.stdlib = (function() {
4166
6616
  "else": {
4167
6617
  type: parse.type('else = % -> bool'),
4168
6618
  compile: function(_) {
4169
- return iTrue;
6619
+ return TRUE;
4170
6620
  }
4171
6621
  },
4172
6622
  not: {
4173
6623
  type: parse.type('not = bool -> bool'),
4174
6624
  compile: function(input) {
4175
- return input.extern('!');
6625
+ return input.op1('!');
4176
6626
  }
4177
6627
  },
4178
6628
  gt: {
@@ -4180,7 +6630,7 @@ stdlib = Gibbon.stdlib = (function() {
4180
6630
  compile: function(input, _arg) {
4181
6631
  var num;
4182
6632
  num = _arg[0];
4183
- return input.binop('>', num);
6633
+ return input.op2('>', num);
4184
6634
  }
4185
6635
  },
4186
6636
  lt: {
@@ -4188,7 +6638,7 @@ stdlib = Gibbon.stdlib = (function() {
4188
6638
  compile: function(input, _arg) {
4189
6639
  var num;
4190
6640
  num = _arg[0];
4191
- return input.binop('<', num);
6641
+ return input.op2('<', num);
4192
6642
  }
4193
6643
  },
4194
6644
  eq: {
@@ -4196,12 +6646,156 @@ stdlib = Gibbon.stdlib = (function() {
4196
6646
  compile: function(input, _arg, tvars) {
4197
6647
  var obj;
4198
6648
  obj = _arg[0];
4199
- return compEquals(input, obj, tvars.get('a'));
6649
+ return equals(input, obj, tvars.get('a'));
6650
+ }
6651
+ },
6652
+ neq: {
6653
+ type: parse.type('neq %a = %a -> bool'),
6654
+ compile: function(input, _arg, tvars) {
6655
+ var obj;
6656
+ obj = _arg[0];
6657
+ return equals(input, obj, tvars.get('a')).op1('!');
4200
6658
  }
4201
6659
  }
4202
6660
  };
4203
6661
  })();
4204
- // Generated by CoffeeScript 1.6.3
6662
+
6663
+ Gibbon.CompiledCode = CompiledCode = (function() {
6664
+ var idFor, trampoline;
6665
+
6666
+ idFor = function(key) {
6667
+ return 'local' + key.replace(/\//g, '$$$$').replace(/-/g, '$$_');
6668
+ };
6669
+
6670
+ trampoline = function(x) {
6671
+ while (typeof x === 'function') {
6672
+ x = x();
6673
+ }
6674
+ return x;
6675
+ };
6676
+
6677
+ function CompiledCode(semantics, blocks) {
6678
+ var _this = this;
6679
+ this.semantics = semantics;
6680
+ this.blocks = blocks;
6681
+ this.functions = new Hash;
6682
+ this.blocks.each(function(k, v) {
6683
+ var func;
6684
+ func = new Function('$', 'QUERY', 'FAIL', 'RETURN', v);
6685
+ return _this.functions.set(k, func);
6686
+ });
6687
+ }
6688
+
6689
+ CompiledCode.prototype.functionFor = function(key) {
6690
+ return this.functions.get(key);
6691
+ };
6692
+
6693
+ CompiledCode.prototype.run = function(rootKey, input, client, cb) {
6694
+ var FAIL, RETURN, dependencies, failures, mkQuery, results, runKey, thunk,
6695
+ _this = this;
6696
+ results = new Hash;
6697
+ dependencies = new Hash;
6698
+ failures = new Hash;
6699
+ runKey = function(key, fail, succeed) {
6700
+ var deps, onFailure, onSuccess, query;
6701
+
6702
+ if (results.has(key)) {
6703
+ return succeed(results.get(key));
6704
+ }
6705
+ if (failures.has(key)) {
6706
+ return fail(failures.get(key));
6707
+ }
6708
+ deps = dependencies.set(key, []);
6709
+ query = mkQuery(function(d) {
6710
+ return deps.push(d);
6711
+ });
6712
+ onSuccess = function(data) {
6713
+ results.set(key, data);
6714
+ return succeed(data);
6715
+ };
6716
+ onFailure = function(err) {
6717
+ failures.set(key, err);
6718
+ return fail(err);
6719
+ };
6720
+ return _this.functionFor(key)(input, query, onFailure, onSuccess);
6721
+ };
6722
+ mkQuery = function(pushDep) {
6723
+ return function(id, annotations, onFailure, onSuccess) {
6724
+ var isSynchronous, out;
6725
+ if (id == null) {
6726
+ pushDep(Dependency.lexical(annotations));
6727
+ return runKey(annotations, onFailure, onSuccess);
6728
+ }
6729
+ isSynchronous = true;
6730
+ out = client.performQuery(id, annotations, function(err, data) {
6731
+ if (err) {
6732
+ pushDep(Dependency.failure(err));
6733
+ if (isSynchronous) {
6734
+ return function() {
6735
+ return onFailure(err);
6736
+ };
6737
+ } else {
6738
+ return trampoline(onFailure(err));
6739
+ }
6740
+ } else {
6741
+ pushDep(Dependency.query(id, annotations));
6742
+ if (isSynchronous) {
6743
+ return function() {
6744
+ return onSuccess(data);
6745
+ };
6746
+ } else {
6747
+ return trampoline(onSuccess(data));
6748
+ }
6749
+ }
6750
+ });
6751
+ isSynchronous = false;
6752
+ return out;
6753
+ };
6754
+ };
6755
+ FAIL = function(f) {
6756
+ return cb(f);
6757
+ };
6758
+ RETURN = function(d) {
6759
+ return cb(null, d);
6760
+ };
6761
+ thunk = runKey(rootKey, cb, function() {
6762
+ var out;
6763
+ out = new Hash;
6764
+ results.each(function(k, v) {
6765
+ var deps, value;
6766
+ value = Value.interpret(v, _this.outputType(k));
6767
+ deps = dependencies.get(k);
6768
+ deps = uniq(deps, function(x, y) {
6769
+ return x.equals(y);
6770
+ });
6771
+ return out.set(k, [value, deps]);
6772
+ });
6773
+ return cb(null, out);
6774
+ });
6775
+ return trampoline(thunk);
6776
+ };
6777
+
6778
+ CompiledCode.prototype.outputType = function(key) {
6779
+ if (key == null) {
6780
+ key = '/';
6781
+ }
6782
+ return this.semantics.get(key).flow.type;
6783
+ };
6784
+
6785
+ return CompiledCode;
6786
+
6787
+ })();
6788
+
6789
+ Gibbon.compile = function(semantics) {
6790
+ var codegen, compiled, optimize, reduce, sequence, translate;
6791
+ codegen = Gibbon.codegen, reduce = Gibbon.reduce, sequence = Gibbon.sequence, optimize = Gibbon.optimize, translate = Gibbon.translate;
6792
+ compiled = new Hash;
6793
+ semantics.each(function(k, v) {
6794
+ return compiled.set(k, codegen(reduce(sequence(optimize(translate(v))))).block.toJS());
6795
+ });
6796
+ return new CompiledCode(semantics, compiled);
6797
+ };
6798
+
4205
6799
  Gibbon.jsonConsumer = (function() {
4206
6800
  return function(tables) {
4207
6801
  var analyzeList, getType, getValue, listLookup, lists;
@@ -4222,7 +6816,7 @@ Gibbon.jsonConsumer = (function() {
4222
6816
  }
4223
6817
  });
4224
6818
  };
4225
- getValue = function(id, annotations, v, callback) {
6819
+ getValue = function(id, annotations, callback) {
4226
6820
  var entity, values;
4227
6821
  if (!tables.hasOwnProperty(annotations.table)) {
4228
6822
  throw new Error("no such type " + annotations.table);
@@ -4235,7 +6829,7 @@ Gibbon.jsonConsumer = (function() {
4235
6829
  if (!(entity.hasOwnProperty(annotations.name) && (entity[annotations.name] != null))) {
4236
6830
  return callback(Failure.query(id, annotations));
4237
6831
  }
4238
- return callback(null, v.fromJSON(entity[annotations.name]));
6832
+ return callback(null, entity[annotations.name]);
4239
6833
  };
4240
6834
  lists = tables._lists || {};
4241
6835
  analyzeList = function(id, listName, t, callback) {
@@ -4254,13 +6848,13 @@ Gibbon.jsonConsumer = (function() {
4254
6848
  }
4255
6849
  });
4256
6850
  };
4257
- listLookup = function(id, listName, v, callback) {
6851
+ listLookup = function(id, listName, callback) {
4258
6852
  var list;
4259
6853
  list = lists[listName].values;
4260
6854
  if (list.indexOf(id) >= 0) {
4261
- return callback(null, v.boolean(true));
6855
+ return callback(null, true);
4262
6856
  } else {
4263
- return callback(null, v.boolean(false));
6857
+ return callback(null, false);
4264
6858
  }
4265
6859
  };
4266
6860
  return {
@@ -4274,11 +6868,11 @@ Gibbon.jsonConsumer = (function() {
4274
6868
  return callback(new Error("unknown query `" + query.type + "'"));
4275
6869
  }
4276
6870
  },
4277
- performQuery: function(id, annotations, v, callback) {
6871
+ performQuery: function(id, annotations, callback) {
4278
6872
  if ('list' in annotations) {
4279
- return listLookup(id, annotations.list, v, callback);
6873
+ return listLookup(id, annotations.list, callback);
4280
6874
  } else {
4281
- return getValue(id, annotations, v, callback);
6875
+ return getValue(id, annotations, callback);
4282
6876
  }
4283
6877
  }
4284
6878
  };