@yaks/logic 0.0.0-stage → 0.2.36
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.
- package/LICENSE +202 -0
- package/README.md +73 -2
- package/_dist/goal.d.ts +50 -0
- package/_dist/goal.d.ts.map +1 -0
- package/_dist/mod.d.ts +4 -0
- package/_dist/mod.d.ts.map +1 -0
- package/_dist/relations.d.ts +48 -0
- package/_dist/relations.d.ts.map +1 -0
- package/_dist/term.d.ts +49 -0
- package/_dist/term.d.ts.map +1 -0
- package/deno.json +1 -0
- package/goal.js +175 -0
- package/goal.js.map +1 -0
- package/goal.ts +284 -0
- package/mod.js +8 -0
- package/mod.js.map +1 -0
- package/mod.ts +7 -0
- package/package.json +16 -4
- package/relations.js +241 -0
- package/relations.js.map +1 -0
- package/relations.ts +357 -0
- package/term.js +176 -0
- package/term.js.map +1 -0
- package/term.ts +250 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright 2026 Jeff Peterson
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
package/README.md
CHANGED
|
@@ -1,3 +1,74 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @yaks/logic
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
A relational runtime in the miniKanren manner. A goal answers every env in which
|
|
4
|
+
it holds, a relation answers from whichever of its arguments are bound, what
|
|
5
|
+
cannot answer yet waits as a constraint, and what needs the world is recorded as
|
|
6
|
+
an effect and performed by a separate pass. Every word is a small function over
|
|
7
|
+
plain values, its examples are the `///` lines above it, and those lines run as
|
|
8
|
+
its tests.
|
|
9
|
+
|
|
10
|
+
```ts
|
|
11
|
+
import { add, and, eq, lt, lvars, member, run } from '@yaks/logic'
|
|
12
|
+
|
|
13
|
+
let { x, y } = lvars
|
|
14
|
+
run(x, add(x, 5, 7)) // [2]
|
|
15
|
+
run(x, and(lt(x, 3), member(x, [1, 2, 3]))) // [1, 2]
|
|
16
|
+
run([x, y], eq([x, 2], [1, y])) // [{ x: 1, y: 2 }]
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
## Terms (`term.ts`)
|
|
20
|
+
|
|
21
|
+
A term is any value: a variable, a scalar, or a list or object holding terms.
|
|
22
|
+
`lvar('x')` is the variable `x` wherever it is written, `lvar()` a fresh one,
|
|
23
|
+
`lvars.x` reads as `lvar('x')`, and `_` matches anything and binds nothing. An
|
|
24
|
+
env maps a variable's name to its value, and binding one answers a copy (`set`).
|
|
25
|
+
`unify(a, b, env)` answers the env that makes two terms equal, or null: lists
|
|
26
|
+
unify item by item, objects key by key, and `splat(xs)` inside a list stands for
|
|
27
|
+
a run of its items (`[x, splat(rest)]`). `deepwalk` reads a term back out with
|
|
28
|
+
its variables replaced.
|
|
29
|
+
|
|
30
|
+
## Goals (`goal.ts`)
|
|
31
|
+
|
|
32
|
+
A goal is `(env) => Iterable<Env>`. `ok` holds once, `fail` never, `eq(a, b)`
|
|
33
|
+
where they unify, `and` where every goal holds, `or` where any does, and
|
|
34
|
+
`oneOf(out, ...values)`. `lets(fn)` gives a goal as many fresh variables as `fn`
|
|
35
|
+
takes, which is how a relation recurs without building its whole search first.
|
|
36
|
+
|
|
37
|
+
`run(vars, goal)` answers every answer for `vars`: a variable's value, or an
|
|
38
|
+
object of each variable's value. `find` answers the first and searches no
|
|
39
|
+
further; `solutions` is the lazy stream both are made of.
|
|
40
|
+
|
|
41
|
+
A relation that cannot answer yet, `lt(x, 1)` before `x` is bound, defers itself
|
|
42
|
+
into the env as a constraint, tried again after each `eq`. The order goals are
|
|
43
|
+
written in never decides whether they hold, and an answer still holding a
|
|
44
|
+
constraint is not an answer. `walked(fn)` builds such a relation: `fn` gets its
|
|
45
|
+
arguments walked and the list of those still unbound.
|
|
46
|
+
|
|
47
|
+
## Relations (`relations.ts`)
|
|
48
|
+
|
|
49
|
+
- Arithmetic, solving for any one unknown: `add`, `sub`, `mul`, `div`, `pow`
|
|
50
|
+
(real roots, both signs for an even power), `sq`.
|
|
51
|
+
- Order: `lt`, `lte`, `gt`, `gte`.
|
|
52
|
+
- Objects: `prop(obj, key, value)`, `merge(a, b, out)` for
|
|
53
|
+
`{ ...a, ...b } = out` in any direction, `assign`, `json`.
|
|
54
|
+
- Lists: `cons`, `first`, `rest`, `empty`, `member`, `length`, `append`, `take`.
|
|
55
|
+
- Types: `type(t, x)` over @yaks/fp's `typeOf`, `number`, `string`, `boolean`,
|
|
56
|
+
`array`, `object`, `isNull`, and `is(x, klass)`.
|
|
57
|
+
- Facts: `facts(...rows)` holds for each row. `relation(name)` keeps its facts
|
|
58
|
+
in the env: a call said (`say`, the default) adds one, a call asked (`ask`)
|
|
59
|
+
holds for each said before.
|
|
60
|
+
|
|
61
|
+
## Effects
|
|
62
|
+
|
|
63
|
+
A goal that needs the world records the function and its arguments in the env
|
|
64
|
+
(`effect`) instead of calling it. `run` leaves such an answer out. `race` is the
|
|
65
|
+
pass that performs the effects, each resolving to a goal that goes on from
|
|
66
|
+
there, and it answers asynchronously as the results arrive, a fast answer before
|
|
67
|
+
a slow one. `request(req, res)` fetches, `response.ok(res)` holds for a response
|
|
68
|
+
that succeeded, and `delay(ms, goal)` waits.
|
|
69
|
+
|
|
70
|
+
## Compatibility
|
|
71
|
+
|
|
72
|
+
Pure functions over plain values; @yaks/fp is its one dependency. `request` uses
|
|
73
|
+
`fetch` and `delay` uses `setTimeout`, only when `race` performs them: any
|
|
74
|
+
JavaScript runtime with the web platform's timers and fetch.
|
package/_dist/goal.d.ts
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { type Slot, type Term, type Var } from "./term.js";
|
|
2
|
+
/**
|
|
3
|
+
* What each variable's name is bound to, and what is kept beside the
|
|
4
|
+
* bindings: the constraints and effects waiting, the facts said. `Env` is the
|
|
5
|
+
* one to start from.
|
|
6
|
+
*/ export type Env = Record<string, Term>;
|
|
7
|
+
/** Every env in which something holds. */ export type Goal = (env: Env) => Iterable<Env>;
|
|
8
|
+
/** A relation: its arguments, as a goal. */ export type Rel = (...args: Term[]) => Goal;
|
|
9
|
+
/**
|
|
10
|
+
* A function and its arguments, called later: a constraint's relation, or an
|
|
11
|
+
* effect's function, which answers the goal to go on with.
|
|
12
|
+
*/ export type Deferred<G = Goal> = [(...args: Term[]) => G, ...Term[]];
|
|
13
|
+
/** Holds, once. */ export declare let ok: Goal;
|
|
14
|
+
/** Never holds. */ export declare let fail: Goal;
|
|
15
|
+
/** `ok` when `cond` holds, `fail` when not. */ export declare let expect: (cond: unknown) => Goal;
|
|
16
|
+
/** A goal of an env's change: it holds once, in the changed env. */ export declare let yields: (fn: (env: Env) => Env | null) => Goal;
|
|
17
|
+
/** Where an env keeps what it defers until its variables are bound. */ export declare let constraints: Var;
|
|
18
|
+
/** Where an env keeps the effects a separate pass performs. */ export declare let effects: Var;
|
|
19
|
+
/** Whether a relation's call says a fact or asks for one. */ export declare let mode: Var;
|
|
20
|
+
/** The env to start from: saying facts, with nothing deferred. */ export declare let Env: Env;
|
|
21
|
+
/** A goal that defers `fn(...args)` until its variables are bound. */ export declare let constraint: (...con: Deferred) => Goal;
|
|
22
|
+
/** A goal that records `fn(...args)` for `race` to perform. */ export declare let effect: (...eff: Deferred<Goal | Promise<Goal>>) => Goal;
|
|
23
|
+
export declare let wipeConstraints: (env: Env) => Env;
|
|
24
|
+
export declare let wipeEffects: (env: Env) => Env;
|
|
25
|
+
export declare let hasConstraints: (env: Env) => boolean;
|
|
26
|
+
export declare let hasEffects: (env: Env) => boolean;
|
|
27
|
+
/** A goal of as many fresh variables as `fn` takes. */ export declare let lets: (fn: (...vars: Var[]) => Goal) => Goal;
|
|
28
|
+
/**
|
|
29
|
+
* A relation of `fn`, given its arguments walked and which of them are still
|
|
30
|
+
* unbound, so it can answer from whichever it knows.
|
|
31
|
+
*/ export declare let walked: (fn: (...xs: Term[]) => (unbound: Var[]) => Goal) => Rel;
|
|
32
|
+
/** Holds where every goal holds, each answer of one going on to the next. */ export declare let and: (...goals: Goal[]) => Goal;
|
|
33
|
+
/** Holds where any goal holds: each one's answers in turn. */ export declare let or: (...goals: Goal[]) => Goal;
|
|
34
|
+
/** Every deferred goal tried again: each defers itself anew while unbound. */ export declare let resolveConstraints: (env: Env) => Iterable<Env>;
|
|
35
|
+
/** Holds where `a` and `b` unify, once the constraints that wakes hold. */ export declare let eq: (a: Term, b: Term) => Goal;
|
|
36
|
+
/** Holds where `out` is one of `values`. */ export declare let oneOf: (out: Term, ...values: Term[]) => Goal;
|
|
37
|
+
/** A goal whose relations say their facts. */ export declare let say: (goal: Goal) => Goal;
|
|
38
|
+
/** A goal whose relations ask for facts already said. */ export declare let ask: (goal: Goal) => Goal;
|
|
39
|
+
/**
|
|
40
|
+
* What an answer says of `vars`: one variable's value, or an object of each
|
|
41
|
+
* variable's value by its name; no variables, the whole env.
|
|
42
|
+
*/ export declare let reify: (vars: Slot | Slot[]) => (env: Env) => Term;
|
|
43
|
+
/** Each answer of `goal` for `vars`, lazily, leaving out any still waiting. */ export declare function solutions(vars: Slot | Slot[], goal: Goal, env?: Env): Generator<Term>;
|
|
44
|
+
/** Every answer of `goal` for `vars`. */ export declare let run: (vars: Slot | Slot[], goal: Goal, env?: Env) => Term[];
|
|
45
|
+
/** The first answer of `goal` for `vars`, and no more of its search. */ export declare let find: (vars: Slot | Slot[], goal: Goal, env?: Env) => Term;
|
|
46
|
+
/**
|
|
47
|
+
* Every answer of `goal` for `vars` with its effects performed, as each
|
|
48
|
+
* arrives: the pass that does what the goals only recorded.
|
|
49
|
+
*/ export declare function race(vars: Slot | Slot[], goal: Goal, env?: Env): AsyncGenerator<Term>;
|
|
50
|
+
//# sourceMappingURL=goal.d.ts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
|
2
|
+
import { type Term } from "./term.js";
|
|
3
|
+
/** `a + b = c`. */ export declare let add: Rel;
|
|
4
|
+
/** `a * b = c`. */ export declare let mul: Rel;
|
|
5
|
+
/** `a ** n = b`, for a whole `n` of at least one; roots are real. */ export declare let pow: Rel;
|
|
6
|
+
/** `a² = b`. */ export declare let sq: (a: Term, b: Term) => Goal;
|
|
7
|
+
/** `a - b = c`. */ export declare let sub: (a: Term, b: Term, c: Term) => Goal;
|
|
8
|
+
/** `a / b = c`. */ export declare let div: (a: Term, b: Term, c: Term) => Goal;
|
|
9
|
+
/** `a < b`, once both are bound. */ export declare let lt: Rel;
|
|
10
|
+
/** `a <= b`, once both are bound. */ export declare let lte: Rel;
|
|
11
|
+
/** `a > b`, once both are bound. */ export declare let gt: (a: Term, b: Term) => Goal;
|
|
12
|
+
/** `a >= b`, once both are bound. */ export declare let gte: (a: Term, b: Term) => Goal;
|
|
13
|
+
/** `obj[key] = value`. */ export declare let prop: Rel;
|
|
14
|
+
/**
|
|
15
|
+
* `{ ...a, ...b } = out`. Given `out` and `a`, a key `a` shares with `out` may
|
|
16
|
+
* have come from either side, so `b` answers each way.
|
|
17
|
+
*/ export declare let merge: Rel;
|
|
18
|
+
/** `Object.assign({}, ...objs) = out`. */ export declare let assign: (out: Term, ...objs: Term[]) => Goal;
|
|
19
|
+
/** `JSON.stringify(obj) = str`. */ export declare let json: Rel;
|
|
20
|
+
/** `[first, ...rest] = out`. */ export declare let cons: (first: Term, rest: Term, out: Term) => Goal;
|
|
21
|
+
/** `out[0] = head`. */ export declare let first: (head: Term, out: Term) => Goal;
|
|
22
|
+
/** `out.slice(1) = tail`, for a list with a first item. */ export declare let rest: (tail: Term, out: Term) => Goal;
|
|
23
|
+
/** `x = []`. */ export declare let empty: (x: Term) => Goal;
|
|
24
|
+
/** `x` is an item of `list`. */ export declare let member: (x: Term, list: Term) => Goal;
|
|
25
|
+
/** `list.length = n`; given `n`, a list of fresh variables. */ export declare let length: Rel;
|
|
26
|
+
/** `[...a, ...b] = out`. */ export declare let append: (a: Term, b: Term, out: Term) => Goal;
|
|
27
|
+
/** `list.slice(0, n) = out`. */ export declare let take: (n: Term, list: Term, out: Term) => Goal;
|
|
28
|
+
/** `typeOf(x) = t` (@yaks/fp's `typeOf`), once `x` is bound. */ export declare let type: Rel;
|
|
29
|
+
export declare let number: (x: Term) => Goal;
|
|
30
|
+
export declare let string: (x: Term) => Goal;
|
|
31
|
+
export declare let boolean: (x: Term) => Goal;
|
|
32
|
+
export declare let array: (x: Term) => Goal;
|
|
33
|
+
export declare let object: (x: Term) => Goal;
|
|
34
|
+
export declare let isNull: (x: Term) => Goal;
|
|
35
|
+
/** `x` is an instance of `klass`: given `x`, its class. */ export declare let is: Rel;
|
|
36
|
+
/** A relation holding for each of `rows`, and nothing else. */ export declare let facts: (...rows: Term[][]) => Rel;
|
|
37
|
+
/**
|
|
38
|
+
* A relation whose facts live in the env under its name: said, a call adds
|
|
39
|
+
* its arguments as a fact; asked, it holds for each fact said before.
|
|
40
|
+
*/ export declare let relation: (name: string) => Rel & {
|
|
41
|
+
isVar: boolean;
|
|
42
|
+
};
|
|
43
|
+
/** `fetch(req)` answers `res`: an effect, performed by `race`. */ export declare let request: Rel;
|
|
44
|
+
/** Relations about a response. */ export declare let response: {
|
|
45
|
+
ok: Rel;
|
|
46
|
+
};
|
|
47
|
+
/** `goal`, after `ms` milliseconds: an effect, performed by `race`. */ export declare let delay: Rel;
|
|
48
|
+
//# sourceMappingURL=relations.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"relations.d.ts","sources":["../relations.ts"],"names":[],"mappings":"AAQA,SAOE,KAAK,IAAI,EAIT,KAAK,GAAG,oBAGQ;AAClB,SAME,KAAK,IAAI,oBAGO;AAQlB,iBAAiB,GACjB,OAAO,YAAI,KAAK,IAUd;AAMF,iBAAiB,GACjB,OAAO,YAAI,KAAK,IAUd;AASF,mEAAmE,GACnE,OAAO,YAAI,KAAK,IAOd;AA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O,KACT;AAG3C,8DAA8D,GAC9D,OAAO,YAAI,MAAM,IAEhB;AAKD,OAAO,YAAI,SAAU,GAAG,SAAO,KAAyB;AACxD,OAAO,YAAI,SAAU,GAAG,SAAO,KAAyB;AACxD,OAAO,YAAI,UAAW,GAAG,SAAO,KAA0B;AAC1D,OAAO,YAAI,QAAS,GAAG,SAAO,KAAwB;AACtD,OAAO,YAAI,SAAU,GAAG,SAAO,KAAyB;AACxD,OAAO,YAAI,SAAU,GAAG,SAAO,KAAuB;AAYtD,yDAAyD,GACzD,OAAO,YAAI,IAAI,IAMd;AAKD,6DAA6D,GAC7D,OAAO,YAAI,QAAS,GAAG,MAAM,aAAW,IACC;AAMzC;;;CAGC,GACD,OAAO,YAAI,WAAY,MAAM,MAAM,KAAG;EAAQ,OAAO,OAAO;EAM3D;AAQD,gEAAgE,GAChE,OAAO,YAAI,SAAS,IAEnB;AAED,gCAAgC,GAChC,OAAO,YAAI;EAAY,IAAI;EAM1B;AAOD,qEAAqE,GACrE,OAAO,YAAI,OAAO,IAEjB"}
|
package/_dist/term.d.ts
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import type { Env } from "./goal.js";
|
|
2
|
+
/** A name for a slot in an env: a variable, or a relation keeping its facts. */ export type Slot = {
|
|
3
|
+
isVar: boolean;
|
|
4
|
+
toString(): string;
|
|
5
|
+
};
|
|
6
|
+
/** Any value: a variable, a scalar, or a list or object holding terms. */ export type Term = any;
|
|
7
|
+
/**
|
|
8
|
+
* A logic variable. A named one is one variable wherever the name is written,
|
|
9
|
+
* so `lvar('x') === lvar('x')`; a fresh one has a name of its own.
|
|
10
|
+
*/ export declare class Var {
|
|
11
|
+
name: string;
|
|
12
|
+
constructor(name: string);
|
|
13
|
+
get isVar(): boolean;
|
|
14
|
+
toString(): string;
|
|
15
|
+
/** The variable named for this one's part `name`: `x.var('a')` is `x.a`. */ var(name: string): Var;
|
|
16
|
+
}
|
|
17
|
+
/** The variable `name`, or a fresh one. */ export declare let lvar: (name?: string) => Var;
|
|
18
|
+
/** `length` fresh variables. */ export declare let nvars: (length: number) => Var[];
|
|
19
|
+
/** Every named variable, by its name: `let { a, b } = lvars`. */ export declare let lvars: Record<string, Var>;
|
|
20
|
+
export declare let x: Var;
|
|
21
|
+
export declare let y: Var;
|
|
22
|
+
export declare let z: Var;
|
|
23
|
+
/** Whether a term names a slot in an env: a variable, or a relation's facts. */ export declare let isVar: (x: Term) => boolean;
|
|
24
|
+
/** `_` unifies with any value, and never binds. */ export declare let _: Var;
|
|
25
|
+
/** `...value` inside a list: a run of the list's items. */ export declare class Splat {
|
|
26
|
+
value: Term;
|
|
27
|
+
constructor(value: Term);
|
|
28
|
+
}
|
|
29
|
+
/** `...value` inside a list pattern or literal. */ export declare let splat: (value: Term) => Splat;
|
|
30
|
+
export declare let isSplat: (x: Term) => x is Splat;
|
|
31
|
+
/** What `key` is bound to, or `def`. */ export declare let get: (key: Term, def?: Term) => any;
|
|
32
|
+
/** The env with `key` bound to `value`; `_` binds nothing. */ export declare let set: (key: Term, value: Term) => any;
|
|
33
|
+
/** The env with `key` bound to `fn` of what it was. */ export declare let update: (key: Term, fn: (v: Term) => Term) => any;
|
|
34
|
+
/** A variable's value, followed through the variables it is bound to. */ export declare let walk: (x: Term, env: Env) => Term;
|
|
35
|
+
export declare let walkAll: (env: Env, xs: Term[]) => Term[];
|
|
36
|
+
/**
|
|
37
|
+
* A term with every variable inside it walked: a splat whose value is a list
|
|
38
|
+
* becomes its items. A plain object is walked into; any other object, a
|
|
39
|
+
* response or a date, is a value of its own.
|
|
40
|
+
*/ export declare let deepwalk: (x: Term, env: Env) => Term;
|
|
41
|
+
/** Whether `v` occurs inside `x`: binding `v` to `x` would never end. */ export declare let occurs: (v: Var, x: Term, env: Env) => boolean;
|
|
42
|
+
/**
|
|
43
|
+
* The env that makes `a` and `b` equal, or null. A variable binds unless it
|
|
44
|
+
* occurs in what it would be bound to; lists unify item by item, a splat
|
|
45
|
+
* taking whatever run of items fits, and objects key by key.
|
|
46
|
+
*/ export declare let unify: (a: Term, b: Term, env: Env) => Env | null;
|
|
47
|
+
/** Two lists unified item by item; a splat on either side takes a run. */ export declare let unifyArrays: (a: Term[], b: Term[], env: Env) => Env | null;
|
|
48
|
+
/** Two objects unified key by key: each has the keys the other has. */ export declare let unifyObjects: (a: Env, b: Env, env: Env) => Env | null;
|
|
49
|
+
//# sourceMappingURL=term.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"term.d.ts","sources":["../term.ts"],"names":[],"mappings":"AAOA,cAAc,GAAG,oBAAmB;AAEpC,8EAA8E,GAC9E,YAAY;EAAS,OAAO,OAAO;EAAE,YAAY,MAAM;;AAEvD,wEAAwE,GAExE,YAAY,OAAO,GAAG;AAEtB;;;CAGC,GACD,OAAO,cAAM;EACX,MAAM,MAAM,CAAA;EACZ,YAAY,MAAM,MAAM;EAGxB,IAAI,SAAS,OAAO;EAGpB,YAAY,MAAM;EAGlB,0EAA0E,GAC1E,IAAI,MAAM,MAAM,GAAG;AAGrB;AAQA,yCAAyC,GACzC,OAAO,YAAI,OAAQ,OAAO,MAAM,KAAG,IAGwC;AAG3E,8BAA8B,GAC9B,OAAO,YAAI,QAAS,QAAQ,MAAM,KAAG,MACC;AAGtC,+DAA+D,GAC/D,OAAO,YAAI,OAAO,OAAO,MAAM,EAAE,KAE/B;AAEF,OAAO,YAAI,GAAG,IAAe;AAC7B,OAAO,YAAI,GAAG,IAAe;AAC7B,OAAO,YAAI,GAAG,IAAe;AAI7B,8EAA8E,GAC9E,OAAO,YAAI,QAAS,GAAG,SAAO,OAAO,CAAqB;AAE1D,iDAAiD,GACjD,OAAO,YAAI,GAAG,IAAe;AAE7B,yDAAyD,GACzD,OAAO,cAAM;EACX,OAAO,KAAI;EACX,YAAY,OAAO,IAAI;AAGzB;AAGA,iDAAiD,GACjD,OAAO,YAAI,QAAS,OAAO,SAAO,MAAyB;AAI3D,OAAO,YAAI,UAAW,GAAG,SAAO,KAAK,MAA2B;AAIhE,sCAAsC,GACtC,OAAO,YAAI,MAAO,KAAK,MAAM,MAAM,aAA4C;AAI/E,4DAA4D,GAC5D,OAAO,YAAI,MAAO,KAAK,MAAM,OAAO,aACgC;AAGpE,qDAAqD,GACrD,OAAO,YAAI,SAAU,KAAK,MAAM,KAAK,GAAG,SAAS,aACpB;AAK7B,uEAAuE,GACvE,OAAO,YAAI,OAAQ,GAAG,MAAM,KAAK,QAAM,KAItC;AAGD,OAAO,YAAI,UAAW,KAAK,KAAK,IAAI,WAAS,OAChB;AAU7B;;;;CAIC,GACD,OAAO,YAAI,WAAY,GAAG,MAAM,KAAK,QAAM,KAgB1C;AAKD,uEAAuE,GACvE,OAAO,YAAI,SAAU,GAAG,KAAK,GAAG,MAAM,KAAK,QAAM,OAAO,CAQvD;AAMD;;;;CAIC,GACD,OAAO,YAAI,QAAS,GAAG,MAAM,GAAG,MAAM,KAAK,QAAM,MAAM,IAAI,CAW1D;AAMD,wEAAwE,GACxE,OAAO,YAAI,cAAe,GAAG,QAAQ,GAAG,QAAQ,KAAK,QAAM,MAAM,IAAI,CAK3D;AA+CV,qEAAqE,GACrE,OAAO,YAAI,eAAgB,GAAG,KAAK,GAAG,KAAK,KAAK,QAAM,MAAM,IAAI,CAM/D"}
|
package/deno.json
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"name":"@yaks/logic","version":"0.2.36","license":"Apache-2.0","description":"A relational runtime in the miniKanren manner: unification over lists and objects, constraints deferred until their variables are bound, relations with facts, and effects kept as data and performed by a separate pass.","exports":"./mod.ts","imports":{"@yaks/logic":"./mod.ts"}}
|
package/goal.js
ADDED
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
// Goals, and running them. A goal takes an env and answers every env in which
|
|
2
|
+
// it holds: none when it fails, several when it has several answers, lazily
|
|
3
|
+
// wherever it searches. `and` threads each answer of one goal through the
|
|
4
|
+
// next, `or` answers each goal's answers in turn, and `eq` unifies.
|
|
5
|
+
//
|
|
6
|
+
// A goal that cannot answer yet defers itself as a constraint: `lt(x, 1)` with
|
|
7
|
+
// `x` unbound is kept in the env and tried again after each `eq`, so the order
|
|
8
|
+
// goals are written in does not decide whether they hold. An answer that still
|
|
9
|
+
// holds a constraint is not an answer, and `run` leaves it out.
|
|
10
|
+
//
|
|
11
|
+
// Effects are data too. A goal that needs the world (a fetch, a timer) records
|
|
12
|
+
// the function and its arguments in the env and answers at once; `run` leaves
|
|
13
|
+
// such an answer out, and `race` is the separate pass that performs the
|
|
14
|
+
// effects, each resolving to a goal that goes on from there, and answers as the
|
|
15
|
+
// results arrive.
|
|
16
|
+
import { compose } from "@yaks/fp";
|
|
17
|
+
import { deepwalk, get, isVar, lvar, nvars, set, unify, update, walkAll } from "./term.js";
|
|
18
|
+
/// [...ok({})] -> [{}]
|
|
19
|
+
/** Holds, once. */ export let ok = (env)=>[
|
|
20
|
+
env
|
|
21
|
+
];
|
|
22
|
+
/// [...fail({})] -> []
|
|
23
|
+
/** Never holds. */ export let fail = ()=>[];
|
|
24
|
+
/** `ok` when `cond` holds, `fail` when not. */ export let expect = (cond)=>cond ? ok : fail;
|
|
25
|
+
/// [...yields(set(x, 1))({})] -> [{ x: 1 }]
|
|
26
|
+
/** A goal of an env's change: it holds once, in the changed env. */ export let yields = (fn)=>(env)=>{
|
|
27
|
+
let next = fn(env);
|
|
28
|
+
return next ? [
|
|
29
|
+
next
|
|
30
|
+
] : [];
|
|
31
|
+
};
|
|
32
|
+
let push = (...added)=>(xs = [])=>[
|
|
33
|
+
...xs,
|
|
34
|
+
...added
|
|
35
|
+
];
|
|
36
|
+
/** Where an env keeps what it defers until its variables are bound. */ export let constraints = lvar('~.constraints');
|
|
37
|
+
/** Where an env keeps the effects a separate pass performs. */ export let effects = lvar('~.effects');
|
|
38
|
+
/** Whether a relation's call says a fact or asks for one. */ export let mode = lvar('~.mode');
|
|
39
|
+
/** The env to start from: saying facts, with nothing deferred. */ export let Env = compose(set(mode, 'say'), set(constraints, []), set(effects, []))({});
|
|
40
|
+
/** A goal that defers `fn(...args)` until its variables are bound. */ export let constraint = (...con)=>yields(update(constraints, push(con)));
|
|
41
|
+
/** A goal that records `fn(...args)` for `race` to perform. */ export let effect = (...eff)=>yields(update(effects, push(eff)));
|
|
42
|
+
export let wipeConstraints = update(constraints, ()=>[]);
|
|
43
|
+
export let wipeEffects = update(effects, ()=>[]);
|
|
44
|
+
export let hasConstraints = (env)=>get(constraints, [])(env).length > 0;
|
|
45
|
+
export let hasEffects = (env)=>get(effects, [])(env).length > 0;
|
|
46
|
+
/// let _x
|
|
47
|
+
/// [...lets((x) => (env) => [_x = x])({})] -> [_x]
|
|
48
|
+
/** A goal of as many fresh variables as `fn` takes. */ export let lets = (fn)=>(env)=>fn(...nvars(fn.length))(env);
|
|
49
|
+
/// let one = walked((x) => (unbound) => unbound.length ? fail : ok)
|
|
50
|
+
/// run(_, one(1)) -> [_]
|
|
51
|
+
/// run(_, one(x)) -> []
|
|
52
|
+
/// one() throws 'Expected 1 arguments'
|
|
53
|
+
/**
|
|
54
|
+
* A relation of `fn`, given its arguments walked and which of them are still
|
|
55
|
+
* unbound, so it can answer from whichever it knows.
|
|
56
|
+
*/ export let walked = (fn)=>(...vars)=>{
|
|
57
|
+
if (vars.length != fn.length) {
|
|
58
|
+
throw new Error(`Expected ${fn.length} arguments, received ${vars.length}`);
|
|
59
|
+
}
|
|
60
|
+
return (env)=>{
|
|
61
|
+
let xs = walkAll(env, vars);
|
|
62
|
+
return fn(...xs)(xs.filter(isVar))(env);
|
|
63
|
+
};
|
|
64
|
+
};
|
|
65
|
+
/// run(x, and(fail, ok)) -> []
|
|
66
|
+
/// run(x, and(ok, fail)) -> []
|
|
67
|
+
/// run(x, and(eq(y, 1), eq(x, y))) -> [1]
|
|
68
|
+
/** Holds where every goal holds, each answer of one going on to the next. */ export let and = (...goals)=>function* conj(env, [goal, ...rest] = goals) {
|
|
69
|
+
if (!goal) yield env;
|
|
70
|
+
else for (let e of goal(env))yield* conj(e, rest);
|
|
71
|
+
};
|
|
72
|
+
/// run(x, or(fail, eq(x, 1))) -> [1]
|
|
73
|
+
/// run(x, or(eq(x, 1), eq(x, 2))) -> [1, 2]
|
|
74
|
+
/** Holds where any goal holds: each one's answers in turn. */ export let or = (...goals)=>function*(env) {
|
|
75
|
+
for (let goal of goals)yield* goal(env);
|
|
76
|
+
};
|
|
77
|
+
/// let waiting = [...lt(x, 1)(Env)][0]
|
|
78
|
+
/// [...resolveConstraints(waiting)] ~> [{ [constraints]: [[lt, x, 1]] }]
|
|
79
|
+
/// [...resolveConstraints(set(x, 0)(waiting))] ~> [{ [constraints]: [], x: 0 }]
|
|
80
|
+
/// run(x, and(lt(x, 1), eq(x, 0))) -> [0]
|
|
81
|
+
/** Every deferred goal tried again: each defers itself anew while unbound. */ export let resolveConstraints = (env)=>and(...get(constraints, [])(env).map(([fn, ...args])=>fn(...args)))(wipeConstraints(env));
|
|
82
|
+
/// run(x, eq({ x }, { x: 1 })) -> [1]
|
|
83
|
+
/// run([x, y], eq([x, y, _], [1, 2, 3])) -> [{ x: 1, y: 2 }]
|
|
84
|
+
/// run(z, eq([[x, y], [y, x]], [[1, 2], z])) -> [[2, 1]]
|
|
85
|
+
/// run(x, eq(x, y)) -> [y]
|
|
86
|
+
/// run(x, eq(x, x)) -> [x]
|
|
87
|
+
/// run(_, eq(1, 1)) -> [_]
|
|
88
|
+
/// run(x, eq(x, [x])) -> []
|
|
89
|
+
/// run(x, eq({ x, y: _ }, { x: 1, y: 2 })) -> [1]
|
|
90
|
+
/// run([x, y], eq([splat(x), 3, splat(y)], [1, 2, 3, 4])) -> [{ x: [1, 2], y: [4] }]
|
|
91
|
+
/** Holds where `a` and `b` unify, once the constraints that wakes hold. */ export let eq = (a, b)=>function*(env) {
|
|
92
|
+
let next = unify(a, b, env);
|
|
93
|
+
if (next) yield* resolveConstraints(next);
|
|
94
|
+
};
|
|
95
|
+
/// run(x, oneOf(x, 1, 2)) -> [1, 2]
|
|
96
|
+
/// run(x, and(add(1, 2, y), oneOf(x, 1, y))) -> [1, 3]
|
|
97
|
+
/** Holds where `out` is one of `values`. */ export let oneOf = (out, ...values)=>or(...values.map((v)=>eq(v, out)));
|
|
98
|
+
/** A goal whose relations say their facts. */ export let say = (goal)=>compose(goal, set(mode, 'say'));
|
|
99
|
+
/** A goal whose relations ask for facts already said. */ export let ask = (goal)=>compose(goal, set(mode, 'ask'));
|
|
100
|
+
/// reify(x)({ x: 1 }) -> 1
|
|
101
|
+
/// reify([x, y])({ x: 1 }) -> { x: 1, y }
|
|
102
|
+
/// reify(1) throws 'is not a variable'
|
|
103
|
+
/**
|
|
104
|
+
* What an answer says of `vars`: one variable's value, or an object of each
|
|
105
|
+
* variable's value by its name; no variables, the whole env.
|
|
106
|
+
*/ export let reify = (vars)=>{
|
|
107
|
+
for (let v of [
|
|
108
|
+
vars
|
|
109
|
+
].flat()){
|
|
110
|
+
if (!isVar(v)) throw new Error(`${v} is not a variable`);
|
|
111
|
+
}
|
|
112
|
+
return !Array.isArray(vars) ? (env)=>deepwalk(vars, env) : !vars.length ? (env)=>env : (env)=>Object.fromEntries(vars.map((v)=>[
|
|
113
|
+
String(v),
|
|
114
|
+
deepwalk(v, env)
|
|
115
|
+
]));
|
|
116
|
+
};
|
|
117
|
+
/** Each answer of `goal` for `vars`, lazily, leaving out any still waiting. */ export function* solutions(vars, goal, env = Env) {
|
|
118
|
+
let result = reify(vars);
|
|
119
|
+
for (let e of goal(env)){
|
|
120
|
+
if (!hasConstraints(e) && !hasEffects(e)) yield result(e);
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
/// run(x, eq(x, 1)) -> [1]
|
|
124
|
+
/// run([x], eq(x, 1)) -> [{ x: 1 }]
|
|
125
|
+
/// run(_, ok) -> [_]
|
|
126
|
+
/// run([], ok) ~> [Object]
|
|
127
|
+
/// run(x, lt(x, 2)) -> []
|
|
128
|
+
/** Every answer of `goal` for `vars`. */ export let run = (vars, goal, env = Env)=>[
|
|
129
|
+
...solutions(vars, goal, env)
|
|
130
|
+
];
|
|
131
|
+
/// find(x, or(eq(x, 1), eq(x, 2))) -> 1
|
|
132
|
+
/// find(x, fail) -> undefined
|
|
133
|
+
/** The first answer of `goal` for `vars`, and no more of its search. */ export let find = (vars, goal, env = Env)=>solutions(vars, goal, env).next().value;
|
|
134
|
+
// The values of every stream, each as it arrives: they all run at once, so a
|
|
135
|
+
// slow one never holds up a fast one.
|
|
136
|
+
async function* merge(streams) {
|
|
137
|
+
let pending = new Map();
|
|
138
|
+
let pull = (it)=>pending.set(it, it.next().then((r)=>[
|
|
139
|
+
it,
|
|
140
|
+
r
|
|
141
|
+
]));
|
|
142
|
+
for (let s of streams)pull(s[Symbol.asyncIterator]());
|
|
143
|
+
while(pending.size){
|
|
144
|
+
let [it, r] = await Promise.race(pending.values());
|
|
145
|
+
if (r.done) pending.delete(it);
|
|
146
|
+
else {
|
|
147
|
+
pull(it);
|
|
148
|
+
yield r.value;
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
function* each(fn, xs) {
|
|
153
|
+
for (let x of xs)yield fn(x);
|
|
154
|
+
}
|
|
155
|
+
// An answer once its effects are performed: each resolves to a goal, and the
|
|
156
|
+
// answers of those, performed in turn, are this answer's.
|
|
157
|
+
async function* settle(env) {
|
|
158
|
+
if (hasEffects(env)) {
|
|
159
|
+
let goals = await Promise.all(get(effects)(env).map(([fn, ...args])=>fn(...args)));
|
|
160
|
+
yield* merge(each(settle, and(...goals)(wipeEffects(env))));
|
|
161
|
+
} else if (!hasConstraints(env)) yield env;
|
|
162
|
+
}
|
|
163
|
+
/// await Array.fromAsync(race(x, delay(0, eq(x, 1)))) -> [1]
|
|
164
|
+
/// await Array.fromAsync(race(x, or(delay(20, eq(x, 1)), delay(0, eq(x, 2))))) -> [2, 1]
|
|
165
|
+
/// await Array.fromAsync(race(x, and(lt(x, 2), delay(0, eq(x, 1))))) -> [1]
|
|
166
|
+
/// await Array.fromAsync(race(x, and(lt(x, 1), delay(0, eq(x, 1))))) -> []
|
|
167
|
+
/// await Array.fromAsync(race([x, y], and(delay(20, eq(x, 1)), delay(0, eq(y, 2))))) -> [{ x: 1, y: 2 }]
|
|
168
|
+
/**
|
|
169
|
+
* Every answer of `goal` for `vars` with its effects performed, as each
|
|
170
|
+
* arrives: the pass that does what the goals only recorded.
|
|
171
|
+
*/ export async function* race(vars, goal, env = Env) {
|
|
172
|
+
let result = reify(vars);
|
|
173
|
+
for await (let e of merge(each(settle, goal(env))))yield result(e);
|
|
174
|
+
}
|
|
175
|
+
//# sourceMappingURL=goal.js.map
|