最終更新 1735237900

capitalex's Avatar capitalex revised this gist 1735237900. Go to revision

2 files changed, 697 insertions

fruitcake-2.chr(file created)

@@ -0,0 +1,16 @@
1 + flour, sugar, apples
2 + <=> apple_cake
3 +
4 + apples, oranges, cherries
5 + <=> fruit_salad
6 +
7 + fruit_salad, apple_cake
8 + <=> fruit_cake
9 +
10 + start <=>
11 + oranges,
12 + apples,
13 + cherries,
14 + flour,
15 + apples,
16 + sugar

fruitcake-2.js(file created)

@@ -0,0 +1,681 @@
1 + /**
2 + *
3 + * Automatically generated
4 + * Do not edit
5 + *
6 + * Created using CHR.js AOT compiler
7 + * (CHR.js version v3.3.21)
8 + * http://github.com/fnogatz/CHR.js
9 + *
10 + */
11 +
12 + module.exports = (function () {
13 +
14 + /* eslint no-labels: ["error", { "allowLoop": true }] */
15 +
16 + // Constraint
17 + function Constraint (name, arity, args) {
18 + this.name = name
19 + this.arity = arity
20 + this.functor = name + '/' + arity
21 + this.args = args
22 + this.id = null
23 + this.alive = true
24 + this.activated = false
25 + this.stored = false
26 + this.hist = null
27 + this.cont = null
28 + }
29 +
30 + Constraint.prototype.continue = function () {
31 + this.cont[0].call(this, this, this.cont[1])
32 + }
33 +
34 + Constraint.prototype.toString = function () {
35 + let s = this.name
36 + if (this.arity >= 1) {
37 + s += '(' + this.args.join(',') + ')'
38 + }
39 + return s
40 + }
41 +
42 + // Store
43 + function Store () {
44 + this._index = {}
45 + this._size = 0
46 + this._nextId = 0
47 + }
48 +
49 + Store.prototype.add = function (constraint) {
50 + if (typeof this._index[constraint.functor] === 'undefined') {
51 + this._index[constraint.functor] = []
52 + }
53 + constraint.id = this._nextId
54 + this._index[constraint.functor].push(constraint)
55 + this._size += 1
56 + this._nextId += 1
57 + }
58 +
59 + Store.prototype.remove = function (constraint) {
60 + constraint.alive = false
61 + const ix = this._index[constraint.functor].indexOf(constraint)
62 + this._index[constraint.functor].splice(ix, 1)
63 +
64 + this._size -= 1
65 + }
66 +
67 + Store.prototype.lookup = function (rule, patterns, constraint) {
68 + const ret = this.lookupResume(rule, patterns, constraint, 0)
69 + if (!ret || !ret.res) {
70 + return false
71 + }
72 + return ret.res
73 + }
74 +
75 + Store.prototype.lookupResume = function (rule, patterns, constraint, startFrom) {
76 + startFrom = startFrom || 0
77 +
78 + const lastPattern = patterns.length - 1
79 + const lengths = []
80 + const divs = []
81 + let div = 1
82 + let i
83 +
84 + // build array of arrays
85 + const arr = []
86 + for (i = 0; i <= lastPattern; i++) {
87 + if (patterns[i] === '_') {
88 + // "_" is a placeholder for the given `constraint`
89 + arr[i] = [constraint]
90 + } else if (typeof this._index[patterns[i]] !== 'undefined') {
91 + arr[i] = this._index[patterns[i]]
92 + } else {
93 + // not a single element for this functor
94 + return false
95 + }
96 + }
97 +
98 + for (i = lastPattern; i >= 0; i--) {
99 + lengths[i] = arr[i].length
100 + divs[i] = div
101 + div *= arr[i].length
102 + }
103 + const max = divs[0] * arr[0].length
104 +
105 + let res
106 + let resIds
107 + let curr
108 + loopng: for (let n = startFrom; n < max; n++) {
109 + res = []
110 + resIds = []
111 + curr = n
112 + for (i = 0; i <= lastPattern; i++) {
113 + res[i] = arr[i][curr / divs[i] >> 0]
114 + resIds[i] = res[i].id
115 +
116 + // avoid multiple occurences of the same constraint
117 + if (res.slice(0, i).indexOf(res[i]) !== -1) {
118 + continue loopng
119 + }
120 +
121 + curr = curr % divs[i]
122 + }
123 +
124 + // check if already in history
125 + /*
126 + if (history.lookup(rule, resIds)) {
127 + continue loopng
128 + }
129 + */
130 + return {
131 + n: n,
132 + res: res
133 + }
134 + }
135 +
136 + return false
137 + }
138 +
139 + Store.prototype.size = function () {
140 + return this._size
141 + }
142 +
143 + Store.prototype.valueOf = function () {
144 + return this.size()
145 + }
146 +
147 + Store.prototype.toString = function () {
148 + if (this.size() === 0) {
149 + return '(empty)'
150 + }
151 +
152 + let maxLengthC = 'constraint'.length
153 + let maxLengthI = 'id'.length
154 + const rows = []
155 + let functor
156 + for (functor in this._index) {
157 + this._index[functor].forEach(function (c) {
158 + const s = c.toString()
159 + maxLengthC = Math.max(s.length, maxLengthC)
160 + maxLengthI = Math.max(c.id.toString().length + 1, maxLengthI)
161 + })
162 + }
163 + for (functor in this._index) {
164 + this._index[functor].forEach(function (c) {
165 + rows.push(c.id.toString().padStart(maxLengthI) + ' | ' + c.toString().padEnd(maxLengthC))
166 + })
167 + }
168 +
169 + return [
170 + 'id'.padStart(maxLengthI) + ' | ' + 'constraint'.padEnd(maxLengthC),
171 + ''.padStart(maxLengthI, '-') + '-+-' + ''.padEnd(maxLengthC, '-')
172 + ].concat(rows).join('\n')
173 + }
174 +
175 + // History
176 + /*
177 + function History () {
178 + this._index = {}
179 + this._size = 0
180 + }
181 +
182 + History.prototype.size = function () {
183 + return this._size
184 + }
185 +
186 + History.prototype.valueOf = function () {
187 + return this.size()
188 + }
189 +
190 + History.prototype.toString = function () {
191 + if (this.size() === 0) {
192 + return "(empty)"
193 + }
194 +
195 + var maxLength_r = "rule".length
196 + var maxLength_f = "fired with".length
197 + var rows = []
198 + var curr
199 + for (var rule in this._index) {
200 + maxLength_r = Math.max(rule.toString().length, maxLength_r)
201 + }
202 +
203 + // TODO
204 + }
205 +
206 + History.prototype.add = function (rule, ids) {
207 + if (!this._index.hasOwnProperty(rule)) {
208 + this._index[rule] = {}
209 + }
210 +
211 + var curr = this._index[rule]
212 + for (var i = 0; i < ids.length-1; i++) {
213 + if (!curr.hasOwnProperty(ids[i])) {
214 + curr[ids[i]] = {}
215 + }
216 + curr = curr[ids[i]]
217 + }
218 + curr[ids[i]] = true
219 +
220 + this._size += 1
221 + }
222 +
223 + History.prototype.lookup = function (rule, ids) {
224 + if (!this._index.hasOwnProperty(rule)) {
225 + return false
226 + }
227 +
228 + var curr = this._index[rule]
229 + for (var i = 0; i < ids.length; i++) {
230 + if (!curr[ids[i]]) {
231 + return false
232 + }
233 + curr = curr[ids[i]]
234 + }
235 +
236 + if (curr !== true) {
237 + return false
238 + }
239 +
240 + return true
241 + }
242 + */
243 + // trampoline
244 + function trampoline () { // eslint-disable-line
245 + let constraint
246 + while (constraint = stack.pop()) { // eslint-disable-line
247 + constraint.continue()
248 + }
249 + }
250 +
251 + var chr = { // eslint-disable-line
252 + Store: new Store()
253 + }
254 +
255 + var stack = [] // eslint-disable-line
256 + // var history = new History()
257 +
258 + function __apples_0_0 (constraint, __n) {
259 + __n = __n || 0
260 +
261 + var constraintPattern = [ "flour/0", "sugar/0", "_" ]
262 + var lookupResult = chr.Store.lookupResume(0, constraintPattern, constraint, __n)
263 + if (lookupResult === false) {
264 + constraint.cont = [__apples_0_1, 0]
265 + stack.push(constraint)
266 + return
267 + }
268 + var constraints = lookupResult.res
269 +
270 + chr.Store.remove(constraints[0])
271 + chr.Store.remove(constraints[1])
272 +
273 + ;(function () {
274 + var _c = new Constraint("apple_cake", 0, [ ])
275 + _c.cont = [__apple_cake_0_0, 0]
276 + stack.push(_c)
277 + })()
278 +
279 + // active constraint gets removed
280 + }
281 +
282 + function __sugar_0_0 (constraint, __n) {
283 + __n = __n || 0
284 +
285 + var constraintPattern = [ "flour/0", "_", "apples/0" ]
286 + var lookupResult = chr.Store.lookupResume(0, constraintPattern, constraint, __n)
287 + if (lookupResult === false) {
288 + constraint.cont = [__sugar_0_1, 0]
289 + stack.push(constraint)
290 + return
291 + }
292 + var constraints = lookupResult.res
293 +
294 + chr.Store.remove(constraints[0])
295 + chr.Store.remove(constraints[2])
296 +
297 + ;(function () {
298 + var _c = new Constraint("apple_cake", 0, [ ])
299 + _c.cont = [__apple_cake_0_0, 0]
300 + stack.push(_c)
301 + })()
302 +
303 + // active constraint gets removed
304 + }
305 +
306 + function __flour_0_0 (constraint, __n) {
307 + __n = __n || 0
308 +
309 + var constraintPattern = [ "_", "sugar/0", "apples/0" ]
310 + var lookupResult = chr.Store.lookupResume(0, constraintPattern, constraint, __n)
311 + if (lookupResult === false) {
312 + constraint.cont = [__flour_0_1, 0]
313 + stack.push(constraint)
314 + return
315 + }
316 + var constraints = lookupResult.res
317 +
318 + chr.Store.remove(constraints[1])
319 + chr.Store.remove(constraints[2])
320 +
321 + ;(function () {
322 + var _c = new Constraint("apple_cake", 0, [ ])
323 + _c.cont = [__apple_cake_0_0, 0]
324 + stack.push(_c)
325 + })()
326 +
327 + // active constraint gets removed
328 + }
329 +
330 + function __cherries_0_0 (constraint, __n) {
331 + __n = __n || 0
332 +
333 + var constraintPattern = [ "apples/0", "oranges/0", "_" ]
334 + var lookupResult = chr.Store.lookupResume(1, constraintPattern, constraint, __n)
335 + if (lookupResult === false) {
336 + constraint.cont = [__cherries_0_1, 0]
337 + stack.push(constraint)
338 + return
339 + }
340 + var constraints = lookupResult.res
341 +
342 + chr.Store.remove(constraints[0])
343 + chr.Store.remove(constraints[1])
344 +
345 + ;(function () {
346 + var _c = new Constraint("fruit_salad", 0, [ ])
347 + _c.cont = [__fruit_salad_0_0, 0]
348 + stack.push(_c)
349 + })()
350 +
351 + // active constraint gets removed
352 + }
353 +
354 + function __oranges_0_0 (constraint, __n) {
355 + __n = __n || 0
356 +
357 + var constraintPattern = [ "apples/0", "_", "cherries/0" ]
358 + var lookupResult = chr.Store.lookupResume(1, constraintPattern, constraint, __n)
359 + if (lookupResult === false) {
360 + constraint.cont = [__oranges_0_1, 0]
361 + stack.push(constraint)
362 + return
363 + }
364 + var constraints = lookupResult.res
365 +
366 + chr.Store.remove(constraints[0])
367 + chr.Store.remove(constraints[2])
368 +
369 + ;(function () {
370 + var _c = new Constraint("fruit_salad", 0, [ ])
371 + _c.cont = [__fruit_salad_0_0, 0]
372 + stack.push(_c)
373 + })()
374 +
375 + // active constraint gets removed
376 + }
377 +
378 + function __apples_0_1 (constraint, __n) {
379 + __n = __n || 0
380 +
381 + var constraintPattern = [ "_", "oranges/0", "cherries/0" ]
382 + var lookupResult = chr.Store.lookupResume(1, constraintPattern, constraint, __n)
383 + if (lookupResult === false) {
384 + constraint.cont = [__apples_0_2, 0]
385 + stack.push(constraint)
386 + return
387 + }
388 + var constraints = lookupResult.res
389 +
390 + chr.Store.remove(constraints[1])
391 + chr.Store.remove(constraints[2])
392 +
393 + ;(function () {
394 + var _c = new Constraint("fruit_salad", 0, [ ])
395 + _c.cont = [__fruit_salad_0_0, 0]
396 + stack.push(_c)
397 + })()
398 +
399 + // active constraint gets removed
400 + }
401 +
402 + function __apple_cake_0_0 (constraint, __n) {
403 + __n = __n || 0
404 +
405 + var constraintPattern = [ "fruit_salad/0", "_" ]
406 + var lookupResult = chr.Store.lookupResume(2, constraintPattern, constraint, __n)
407 + if (lookupResult === false) {
408 + constraint.cont = [__apple_cake_0_1, 0]
409 + stack.push(constraint)
410 + return
411 + }
412 + var constraints = lookupResult.res
413 +
414 + chr.Store.remove(constraints[0])
415 +
416 + ;(function () {
417 + var _c = new Constraint("fruit_cake", 0, [ ])
418 + _c.cont = [__fruit_cake_0_0, 0]
419 + stack.push(_c)
420 + })()
421 +
422 + // active constraint gets removed
423 + }
424 +
425 + function __fruit_salad_0_0 (constraint, __n) {
426 + __n = __n || 0
427 +
428 + var constraintPattern = [ "_", "apple_cake/0" ]
429 + var lookupResult = chr.Store.lookupResume(2, constraintPattern, constraint, __n)
430 + if (lookupResult === false) {
431 + constraint.cont = [__fruit_salad_0_1, 0]
432 + stack.push(constraint)
433 + return
434 + }
435 + var constraints = lookupResult.res
436 +
437 + chr.Store.remove(constraints[1])
438 +
439 + ;(function () {
440 + var _c = new Constraint("fruit_cake", 0, [ ])
441 + _c.cont = [__fruit_cake_0_0, 0]
442 + stack.push(_c)
443 + })()
444 +
445 + // active constraint gets removed
446 + }
447 +
448 + function __start_0_0 (constraint, __n) {
449 + __n = __n || 0
450 +
451 + ;(function () {
452 + var _c = new Constraint("oranges", 0, [ ])
453 + _c.cont = [__oranges_0_0, 0]
454 + stack.push(_c)
455 + })()
456 +
457 + ;(function () {
458 + var _c = new Constraint("apples", 0, [ ])
459 + _c.cont = [__apples_0_0, 0]
460 + stack.push(_c)
461 + })()
462 +
463 + ;(function () {
464 + var _c = new Constraint("cherries", 0, [ ])
465 + _c.cont = [__cherries_0_0, 0]
466 + stack.push(_c)
467 + })()
468 +
469 + ;(function () {
470 + var _c = new Constraint("flour", 0, [ ])
471 + _c.cont = [__flour_0_0, 0]
472 + stack.push(_c)
473 + })()
474 +
475 + ;(function () {
476 + var _c = new Constraint("apples", 0, [ ])
477 + _c.cont = [__apples_0_0, 0]
478 + stack.push(_c)
479 + })()
480 +
481 + ;(function () {
482 + var _c = new Constraint("sugar", 0, [ ])
483 + _c.cont = [__sugar_0_0, 0]
484 + stack.push(_c)
485 + })()
486 +
487 + // active constraint gets removed
488 + }
489 +
490 + function __flour_0_1 (constraint) {
491 + constraint.cont = null
492 + chr.Store.add(constraint)
493 + }
494 +
495 + function __sugar_0_1 (constraint) {
496 + constraint.cont = null
497 + chr.Store.add(constraint)
498 + }
499 +
500 + function __apples_0_2 (constraint) {
501 + constraint.cont = null
502 + chr.Store.add(constraint)
503 + }
504 +
505 + function __apple_cake_0_1 (constraint) {
506 + constraint.cont = null
507 + chr.Store.add(constraint)
508 + }
509 +
510 + function __oranges_0_1 (constraint) {
511 + constraint.cont = null
512 + chr.Store.add(constraint)
513 + }
514 +
515 + function __cherries_0_1 (constraint) {
516 + constraint.cont = null
517 + chr.Store.add(constraint)
518 + }
519 +
520 + function __fruit_salad_0_1 (constraint) {
521 + constraint.cont = null
522 + chr.Store.add(constraint)
523 + }
524 +
525 + function __fruit_cake_0_0 (constraint) {
526 + constraint.cont = null
527 + chr.Store.add(constraint)
528 + }
529 +
530 + function __start_0_1 (constraint) {
531 + constraint.cont = null
532 + chr.Store.add(constraint)
533 + }
534 +
535 + function flour () {
536 + var args = Array.prototype.slice.call(arguments)
537 + var arity = arguments.length
538 + var functor = "flour/" + arity
539 + var constraint = new Constraint("flour", arity, args)
540 + if (arity === 0) {
541 + constraint.cont = [__flour_0_0, ]
542 + } else {
543 + throw new Error("Undefined constraint: " + functor)
544 + }
545 + stack.push(constraint)
546 +
547 + trampoline()
548 + }
549 +
550 + function sugar () {
551 + var args = Array.prototype.slice.call(arguments)
552 + var arity = arguments.length
553 + var functor = "sugar/" + arity
554 + var constraint = new Constraint("sugar", arity, args)
555 + if (arity === 0) {
556 + constraint.cont = [__sugar_0_0, ]
557 + } else {
558 + throw new Error("Undefined constraint: " + functor)
559 + }
560 + stack.push(constraint)
561 +
562 + trampoline()
563 + }
564 +
565 + function apples () {
566 + var args = Array.prototype.slice.call(arguments)
567 + var arity = arguments.length
568 + var functor = "apples/" + arity
569 + var constraint = new Constraint("apples", arity, args)
570 + if (arity === 0) {
571 + constraint.cont = [__apples_0_0, ]
572 + } else {
573 + throw new Error("Undefined constraint: " + functor)
574 + }
575 + stack.push(constraint)
576 +
577 + trampoline()
578 + }
579 +
580 + function apple_cake () {
581 + var args = Array.prototype.slice.call(arguments)
582 + var arity = arguments.length
583 + var functor = "apple_cake/" + arity
584 + var constraint = new Constraint("apple_cake", arity, args)
585 + if (arity === 0) {
586 + constraint.cont = [__apple_cake_0_0, ]
587 + } else {
588 + throw new Error("Undefined constraint: " + functor)
589 + }
590 + stack.push(constraint)
591 +
592 + trampoline()
593 + }
594 +
595 + function oranges () {
596 + var args = Array.prototype.slice.call(arguments)
597 + var arity = arguments.length
598 + var functor = "oranges/" + arity
599 + var constraint = new Constraint("oranges", arity, args)
600 + if (arity === 0) {
601 + constraint.cont = [__oranges_0_0, ]
602 + } else {
603 + throw new Error("Undefined constraint: " + functor)
604 + }
605 + stack.push(constraint)
606 +
607 + trampoline()
608 + }
609 +
610 + function cherries () {
611 + var args = Array.prototype.slice.call(arguments)
612 + var arity = arguments.length
613 + var functor = "cherries/" + arity
614 + var constraint = new Constraint("cherries", arity, args)
615 + if (arity === 0) {
616 + constraint.cont = [__cherries_0_0, ]
617 + } else {
618 + throw new Error("Undefined constraint: " + functor)
619 + }
620 + stack.push(constraint)
621 +
622 + trampoline()
623 + }
624 +
625 + function fruit_salad () {
626 + var args = Array.prototype.slice.call(arguments)
627 + var arity = arguments.length
628 + var functor = "fruit_salad/" + arity
629 + var constraint = new Constraint("fruit_salad", arity, args)
630 + if (arity === 0) {
631 + constraint.cont = [__fruit_salad_0_0, ]
632 + } else {
633 + throw new Error("Undefined constraint: " + functor)
634 + }
635 + stack.push(constraint)
636 +
637 + trampoline()
638 + }
639 +
640 + function fruit_cake () {
641 + var args = Array.prototype.slice.call(arguments)
642 + var arity = arguments.length
643 + var functor = "fruit_cake/" + arity
644 + var constraint = new Constraint("fruit_cake", arity, args)
645 + if (arity === 0) {
646 + constraint.cont = [__fruit_cake_0_0, ]
647 + } else {
648 + throw new Error("Undefined constraint: " + functor)
649 + }
650 + stack.push(constraint)
651 +
652 + trampoline()
653 + }
654 +
655 + function start () {
656 + var args = Array.prototype.slice.call(arguments)
657 + var arity = arguments.length
658 + var functor = "start/" + arity
659 + var constraint = new Constraint("start", arity, args)
660 + if (arity === 0) {
661 + constraint.cont = [__start_0_0, ]
662 + } else {
663 + throw new Error("Undefined constraint: " + functor)
664 + }
665 + stack.push(constraint)
666 +
667 + trampoline()
668 + }
669 +
670 + chr.flour = flour
671 + chr.sugar = sugar
672 + chr.apples = apples
673 + chr.apple_cake = apple_cake
674 + chr.oranges = oranges
675 + chr.cherries = cherries
676 + chr.fruit_salad = fruit_salad
677 + chr.fruit_cake = fruit_cake
678 + chr.start = start
679 +
680 + return chr
681 + })()
Newer Older