inets 0.1.0__tar.gz
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.
- inets-0.1.0/LICENSE +21 -0
- inets-0.1.0/PKG-INFO +286 -0
- inets-0.1.0/README.md +265 -0
- inets-0.1.0/pyproject.toml +42 -0
- inets-0.1.0/setup.cfg +4 -0
- inets-0.1.0/src/inets/__init__.py +25 -0
- inets-0.1.0/src/inets/__main__.py +104 -0
- inets-0.1.0/src/inets/errors.py +22 -0
- inets-0.1.0/src/inets/ivm.py +205 -0
- inets-0.1.0/src/inets/ivy.py +188 -0
- inets-0.1.0/src/inets/net.py +470 -0
- inets-0.1.0/src/inets/ski.py +382 -0
- inets-0.1.0/src/inets/system.py +542 -0
- inets-0.1.0/src/inets/systems/__init__.py +0 -0
- inets-0.1.0/src/inets/systems/combinators.rules +36 -0
- inets-0.1.0/src/inets/systems/ivm.rules +112 -0
- inets-0.1.0/src/inets/systems/lazy.rules +250 -0
- inets-0.1.0/src/inets/systems/ski.rules +150 -0
- inets-0.1.0/src/inets.egg-info/PKG-INFO +286 -0
- inets-0.1.0/src/inets.egg-info/SOURCES.txt +28 -0
- inets-0.1.0/src/inets.egg-info/dependency_links.txt +1 -0
- inets-0.1.0/src/inets.egg-info/entry_points.txt +2 -0
- inets-0.1.0/src/inets.egg-info/requires.txt +3 -0
- inets-0.1.0/src/inets.egg-info/top_level.txt +1 -0
- inets-0.1.0/tests/test_cli.py +22 -0
- inets-0.1.0/tests/test_ivm.py +46 -0
- inets-0.1.0/tests/test_ivy.py +57 -0
- inets-0.1.0/tests/test_net.py +140 -0
- inets-0.1.0/tests/test_ski.py +92 -0
- inets-0.1.0/tests/test_system.py +94 -0
inets-0.1.0/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 N. E. Davis
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
inets-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,286 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: inets
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Interaction nets: declare a system as a table of rules, check it, and reduce nets by it
|
|
5
|
+
Author: N. E. Davis
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Project-URL: Repository, https://github.com/sigilante/inets
|
|
8
|
+
Keywords: interaction nets,interaction combinators,combinatory logic,graph rewriting,Vine,IVM
|
|
9
|
+
Classifier: Development Status :: 3 - Alpha
|
|
10
|
+
Classifier: Intended Audience :: Education
|
|
11
|
+
Classifier: Intended Audience :: Science/Research
|
|
12
|
+
Classifier: Programming Language :: Python :: 3
|
|
13
|
+
Classifier: Topic :: Scientific/Engineering
|
|
14
|
+
Classifier: Topic :: Education
|
|
15
|
+
Requires-Python: >=3.10
|
|
16
|
+
Description-Content-Type: text/markdown
|
|
17
|
+
License-File: LICENSE
|
|
18
|
+
Provides-Extra: test
|
|
19
|
+
Requires-Dist: pytest; extra == "test"
|
|
20
|
+
Dynamic: license-file
|
|
21
|
+
|
|
22
|
+
# inets
|
|
23
|
+
|
|
24
|
+
Interaction nets in Python: declare an interaction system as a table of rules, check the
|
|
25
|
+
table, and reduce nets by it under a schedule you choose.
|
|
26
|
+
|
|
27
|
+
It is a reference and a teaching tool. It is small, it has no dependencies, and it is not
|
|
28
|
+
fast. What it says about a system rests on checks of the table and on tests, not on proof.
|
|
29
|
+
|
|
30
|
+
```sh
|
|
31
|
+
pip install inets # Python 3.10 or later
|
|
32
|
+
inets demo
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
Or from source, with the tests:
|
|
36
|
+
|
|
37
|
+
```sh
|
|
38
|
+
git clone https://github.com/sigilante/inets && cd inets
|
|
39
|
+
pip install -e '.[test]'
|
|
40
|
+
pytest
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
## One term, three ends
|
|
44
|
+
|
|
45
|
+
An *interaction net* is a graph of nodes, called agents. Each agent has one *principal
|
|
46
|
+
port*, through which it interacts, and some auxiliary ports. Where a wire joins two
|
|
47
|
+
principal ports there is an *active pair*, and a rule replaces the pair by a small net.
|
|
48
|
+
That is all a net can do.
|
|
49
|
+
|
|
50
|
+
The rules do not say which active pair to fire first. For rules that look at nothing but
|
|
51
|
+
their pair, the order does not matter to the result: every order that stops, stops at the
|
|
52
|
+
same net after the same number of interactions. That is a theorem about interaction nets,
|
|
53
|
+
which this package relies on and does not prove. What the order does decide is *whether*
|
|
54
|
+
the net stops. `inets demo` shows it on one term of the combinators S, K and I: `K I Ω`,
|
|
55
|
+
where `Ω` reduces to itself without end and `K` discards it.
|
|
56
|
+
|
|
57
|
+
```
|
|
58
|
+
ski table, by need: only the pairs the answer depends on
|
|
59
|
+
answer I; 2 interactions, of which 1 contractions; stopped because: wanted; active pairs left: 2
|
|
60
|
+
ski table, every pair fired: Ω runs although K will discard it
|
|
61
|
+
answer none: stopped at the cap of 300 interactions; 301 interactions, of which 149 contractions; stopped because: interactions; active pairs left: 3
|
|
62
|
+
lazy table, every pair fired: the demand is carried in the net, and no work is shared
|
|
63
|
+
answer I; 18 interactions, of which 1 contractions; stopped because: normal; active pairs left: 0
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
Every run says why it stopped. `normal` means no active pair is left. `wanted` means
|
|
67
|
+
reduction by need has what it was asked for and has left other pairs alone: what it reaches
|
|
68
|
+
is a normal form of the part of the net the answer can reach, not of the net.
|
|
69
|
+
|
|
70
|
+
## A system is a table
|
|
71
|
+
|
|
72
|
+
This is a whole table, for the symmetric interaction combinators. It comes with the
|
|
73
|
+
package as `combinators`.
|
|
74
|
+
|
|
75
|
+
```
|
|
76
|
+
agent con 2 # a constructor: two auxiliary ports
|
|
77
|
+
agent dup 2 # a duplicator
|
|
78
|
+
agent era 0 # an eraser
|
|
79
|
+
|
|
80
|
+
rule con (a b) >< con (c d) # two of a kind annihilate
|
|
81
|
+
=> a = c; b = d
|
|
82
|
+
count annihilate
|
|
83
|
+
|
|
84
|
+
rule dup (a b) >< dup (c d)
|
|
85
|
+
=> a = c; b = d
|
|
86
|
+
count annihilate
|
|
87
|
+
|
|
88
|
+
rule con (a b) >< dup (c d) # two of different kinds copy each other
|
|
89
|
+
=> a = dup (p q); b = dup (r s); c = con (p r); d = con (q s)
|
|
90
|
+
count commute
|
|
91
|
+
|
|
92
|
+
rule era >< con (a b) # an eraser erases, and sends erasers on
|
|
93
|
+
=> a = era; b = era
|
|
94
|
+
count erase
|
|
95
|
+
|
|
96
|
+
rule era >< dup (a b)
|
|
97
|
+
=> a = era; b = era
|
|
98
|
+
count erase
|
|
99
|
+
|
|
100
|
+
rule era >< era
|
|
101
|
+
=> nothing
|
|
102
|
+
count erase
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
In the right-hand side of a rule a letter bound on the left is the port that was joined
|
|
106
|
+
there, any other letter is a new wire and occurs exactly twice, and `dup (p q)` is a new
|
|
107
|
+
node standing for its principal port. `count` names the statistic the rule adds to.
|
|
108
|
+
|
|
109
|
+
A table may also give each port a sign, `+` for an output and `-` for an input, as the
|
|
110
|
+
tables for S, K and I do:
|
|
111
|
+
|
|
112
|
+
```
|
|
113
|
+
agent S2 + - - # S x y: an output, and two inputs for x and y
|
|
114
|
+
agent app - - + # application: (function) (argument result)
|
|
115
|
+
|
|
116
|
+
rule app (z r) >< S2 (x y)
|
|
117
|
+
=> z = dup (p q); x = app (p f); y = app (q g); f = app (g r)
|
|
118
|
+
count contract
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
The full format, with conditions, references to named nets and calls to a host, is in the
|
|
122
|
+
docstring of [`inets.system`](src/inets/system.py).
|
|
123
|
+
|
|
124
|
+
```python
|
|
125
|
+
import inets
|
|
126
|
+
|
|
127
|
+
system = inets.bundled("combinators") # or inets.System.load("a-table-of-yours.rules")
|
|
128
|
+
print(system.check()) # [] if no problem was found
|
|
129
|
+
|
|
130
|
+
net = inets.Net(system)
|
|
131
|
+
a, b = net.free(), net.free() # two free ports: loose ends to attach to or read from
|
|
132
|
+
net.wire(net.make("con", a, b), net.make("dup", net.make("era"), net.make("era")))
|
|
133
|
+
result = net.reduce() # or net.reduce_in_rounds(), or net.reduce_by_need(port)
|
|
134
|
+
print(result.reason, result.counts) # normal {'commute': 1, 'erase': 2}
|
|
135
|
+
print(net.dot()) # the net as Graphviz text
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
`net.make(agent, *ports, param=value)` makes a node, joins its auxiliary ports to the ports
|
|
139
|
+
given, and returns its principal port; `net.wire` joins two ports. A port is a node's
|
|
140
|
+
number and an index, 0 for the principal port. To read a result, follow a free port:
|
|
141
|
+
`net.link[port]` is the port it is joined to, and `net.kind[node]` and `net.param[node]`
|
|
142
|
+
say what is there. A table whose rules expand named nets or call functions needs a host:
|
|
143
|
+
`inets.Net(system, inets.Library({name: build}))` supplies named nets, and a subclass of
|
|
144
|
+
`inets.Host` supplies functions.
|
|
145
|
+
|
|
146
|
+
## What checking a table means
|
|
147
|
+
|
|
148
|
+
`system.check()`, or `inets check TABLE`, reports each problem as a sentence. For any
|
|
149
|
+
table:
|
|
150
|
+
|
|
151
|
+
- a rule with no body or no count, or one that uses a parameter or a port it does not
|
|
152
|
+
have, gives an agent a parameter it does not take, or names two ports with one letter;
|
|
153
|
+
- a rule whose right-hand side does not use every auxiliary port of its pair exactly once,
|
|
154
|
+
or leaves a port of a new node unjoined: the net it makes must have the pair's free
|
|
155
|
+
ports;
|
|
156
|
+
- a rule that can never apply, because an earlier one for the same pair always does;
|
|
157
|
+
- a pair whose rules have conditions that cannot be seen to cover every case (end with
|
|
158
|
+
`otherwise`);
|
|
159
|
+
- a test of a neighbour on a port or of an agent that does not exist, and an `expand` of
|
|
160
|
+
an agent that has auxiliary ports.
|
|
161
|
+
|
|
162
|
+
For a table without signs, also: a pair of agents with no rule.
|
|
163
|
+
|
|
164
|
+
For a table with signs, also: an agent without them; a rule not written consumer `><`
|
|
165
|
+
value (a consumer's principal port is an input, a value's an output); a consumer and value
|
|
166
|
+
with no rule, or a rule between two values or two consumers; and a rule that joins two
|
|
167
|
+
outputs or two inputs. If no rule does that, a net whose wires each join an output to an
|
|
168
|
+
input stays that way, so the only principal ports that can meet are a consumer's and a
|
|
169
|
+
value's, and each such pair has a rule.
|
|
170
|
+
|
|
171
|
+
It does not decide whether a system terminates, and it does not show that a rule is the
|
|
172
|
+
one intended: S with its arguments exchanged passes every check. `inets rules TABLE` marks
|
|
173
|
+
each rule that is *not* a local rule: one that looks at a neighbour, expands a named net,
|
|
174
|
+
calls the host, or keeps one of its pair. The theorem about order is for local rules.
|
|
175
|
+
|
|
176
|
+
## Three schedules
|
|
177
|
+
|
|
178
|
+
| Method | Fires | Needs | Stops with |
|
|
179
|
+
|---|---|---|---|
|
|
180
|
+
| `net.reduce(order=...)` | one active pair at a time: `"stack"` (newest first), `"queue"` (oldest first) or `"random"` | any table | `normal` |
|
|
181
|
+
| `net.reduce_in_rounds()` | every active pair, round by round | any table | `normal`, with the number of rounds and the widest round |
|
|
182
|
+
| `net.reduce_by_need(port)` | only the pairs that the value wanted at `port` depends on | a table with signs | `wanted`, with the active pairs it left |
|
|
183
|
+
|
|
184
|
+
Each takes `max_interactions` and stops with `interactions` if it gets there; a run in
|
|
185
|
+
rounds looks at the cap between rounds, so it may pass it by less than a round. The number
|
|
186
|
+
of rounds is the net's span: the longest chain of interactions that must follow one
|
|
187
|
+
another. A result's counts are for that run. Any schedule raises `inets.Stuck` at a pair
|
|
188
|
+
no rule applies to; reduction by need raises it too if the value wanted depends on a free
|
|
189
|
+
port.
|
|
190
|
+
|
|
191
|
+
Reduction by need never fires the pairs that would erase what is no longer wanted. So that
|
|
192
|
+
a long run does not fill up, it sweeps the net when it passes 20,000 nodes, and again each
|
|
193
|
+
time the net reaches twice the size the last sweep left (`sweep_above` sets the figure):
|
|
194
|
+
`net.sweep(ports)` keeps what those ports can reach and removes the rest. Sweeping needs a
|
|
195
|
+
table with exactly one eraser, an agent whose only port is an input; without one,
|
|
196
|
+
`net.sweep` raises an error and reduction by need goes on unswept. `deep=False` stops
|
|
197
|
+
reduction by need as soon as the value wanted is a value, its own inputs not asked for.
|
|
198
|
+
|
|
199
|
+
## The tables that come with it
|
|
200
|
+
|
|
201
|
+
| `inets.bundled(...)` | What | Agents, rules |
|
|
202
|
+
|---|---|---|
|
|
203
|
+
| `"combinators"` | The symmetric interaction combinators: a constructor, a duplicator, an eraser | 3, 6 |
|
|
204
|
+
| `"ski"` | The combinators S, K and I as agents, with signs. An application fires as soon as its function is a value | 12, 27 |
|
|
205
|
+
| `"lazy"` | The same with the demand for evaluation carried by agents in the net, so that every pair can be fired and only wanted work is done; no work is shared | 15, 50 |
|
|
206
|
+
| `"ivm"` | The interaction system of the IVM, the machine that runs [Vine](https://github.com/VineLang/vine), read from its source | 5, 20 |
|
|
207
|
+
|
|
208
|
+
[`inets.ski`](src/inets/ski.py) reads and writes terms of S, K and I, builds them as nets,
|
|
209
|
+
names their subterms as references to nets, and reduces them in the three ways of the demo
|
|
210
|
+
(`inets run ski TERM`, with `--rounds` or `--lazy`, and `--dot` to print the net that is
|
|
211
|
+
left as Graphviz text):
|
|
212
|
+
|
|
213
|
+
```python
|
|
214
|
+
from inets import ski
|
|
215
|
+
run = ski.run("```SII0") # S I I 0, by need
|
|
216
|
+
ski.show(run.term), run.result.counts # ('`00', {'collect': 3, 'contract': 3, 'copy': 1})
|
|
217
|
+
```
|
|
218
|
+
|
|
219
|
+
[`inets.ivm`](src/inets/ivm.py) runs programs written in Ivy, Vine's text for nets, by the
|
|
220
|
+
`ivm` table (`inets run ivm FILE.iv`). For the thirteen programs in `tests/ivm`, the tests
|
|
221
|
+
hold it, in three orders of reduction, to the output, the six interaction counts and the
|
|
222
|
+
error flags that `ivm run` gave when they were recorded; with `IVM` set they compare
|
|
223
|
+
output and counts with the binary directly. Six programs compiled from Vine's own
|
|
224
|
+
examples, and the IVM's example `hi.iv`, agreed in the same way in a run that is not kept
|
|
225
|
+
here and cannot be repeated from this repository. It supplies twenty extrinsic functions (N32 arithmetic and comparison, the IVM's
|
|
226
|
+
branch, `print_char`, and the IO value's split and merge) and stops at any other.
|
|
227
|
+
|
|
228
|
+
[`inets.ivy`](src/inets/ivy.py) reads Ivy text for any system.
|
|
229
|
+
|
|
230
|
+
`inets --help` lists the commands.
|
|
231
|
+
|
|
232
|
+
## In a notebook
|
|
233
|
+
|
|
234
|
+
[`kernel/`](kernel) is a second small package, `inets-kernel`: a Jupyter kernel in which a
|
|
235
|
+
cell is a table, a net, or a command. A net is written as the right-hand side of a rule
|
|
236
|
+
is, and is drawn before and after it is reduced, with the active pairs marked.
|
|
237
|
+
|
|
238
|
+
```sh
|
|
239
|
+
pip install inets-kernel
|
|
240
|
+
python -m inets_kernel.install --sys-prefix # or --user
|
|
241
|
+
jupyter lab notebooks/
|
|
242
|
+
```
|
|
243
|
+
|
|
244
|
+
[`notebooks/`](notebooks) has three to start from: the three rules of the combinators;
|
|
245
|
+
building a system of your own, for adding numbers; and one term reaching three ends. They
|
|
246
|
+
are written and run by `notebooks/make.py`, so what they show is what the kernel gives.
|
|
247
|
+
The pictures need Graphviz's `dot`.
|
|
248
|
+
|
|
249
|
+
## Tests
|
|
250
|
+
|
|
251
|
+
```sh
|
|
252
|
+
pytest # about ten seconds; 13 tests are skipped unless IVM is set
|
|
253
|
+
IVM=/path/to/ivm pytest # also compares with an ivm binary directly
|
|
254
|
+
```
|
|
255
|
+
|
|
256
|
+
The tests cover the checks on the bundled tables and on deliberately broken ones; small
|
|
257
|
+
nets of the combinators, with one net reduced in every order; the Ivy reader; the command
|
|
258
|
+
line; the IVM programs against counts recorded from `ivm run` (VineLang/vine at commit
|
|
259
|
+
`cf0c325`); and a census of S, K and I. The census takes every term up to size 5 (size 4
|
|
260
|
+
with two atoms) that has a normal form by ordinary term rewriting within its caps. By need
|
|
261
|
+
and by the `lazy` table each reaches that normal form, the `lazy` table in exactly as many
|
|
262
|
+
contractions as rewriting takes steps and by need in no more. With every pair fired, the
|
|
263
|
+
`ski` table reaches it or runs into the cap.
|
|
264
|
+
|
|
265
|
+
## What it is not
|
|
266
|
+
|
|
267
|
+
- **Fast.** A net is Python dictionaries: roughly a hundred thousand interactions a
|
|
268
|
+
second, measured by hand on one laptop.
|
|
269
|
+
- **A language.** There is a table format and a Python interface, and nothing above them.
|
|
270
|
+
- **A proof.** Checks and tests.
|
|
271
|
+
- **Complete for the IVM.** Memory, depth, workers and most extrinsic functions are left
|
|
272
|
+
out.
|
|
273
|
+
|
|
274
|
+
## Where it comes from
|
|
275
|
+
|
|
276
|
+
The tables, the checks and the machine were worked out in
|
|
277
|
+
[vine-aviary](https://github.com/sigilante/vine-aviary), which has the measurements behind
|
|
278
|
+
the `ski` and `lazy` tables and the argument for why a net of agents cannot both stop when
|
|
279
|
+
it should and share work. This package is that code made general. It does not yet replace
|
|
280
|
+
it: vine-aviary's own machine also removes a duplicator that has lost one of its copies,
|
|
281
|
+
where this one gives the lost copy to an eraser, so its counts of copies under reduction
|
|
282
|
+
by need should be lower than this package's (the two have not been compared).
|
|
283
|
+
|
|
284
|
+
## Licence
|
|
285
|
+
|
|
286
|
+
MIT. See [LICENSE](LICENSE).
|
inets-0.1.0/README.md
ADDED
|
@@ -0,0 +1,265 @@
|
|
|
1
|
+
# inets
|
|
2
|
+
|
|
3
|
+
Interaction nets in Python: declare an interaction system as a table of rules, check the
|
|
4
|
+
table, and reduce nets by it under a schedule you choose.
|
|
5
|
+
|
|
6
|
+
It is a reference and a teaching tool. It is small, it has no dependencies, and it is not
|
|
7
|
+
fast. What it says about a system rests on checks of the table and on tests, not on proof.
|
|
8
|
+
|
|
9
|
+
```sh
|
|
10
|
+
pip install inets # Python 3.10 or later
|
|
11
|
+
inets demo
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
Or from source, with the tests:
|
|
15
|
+
|
|
16
|
+
```sh
|
|
17
|
+
git clone https://github.com/sigilante/inets && cd inets
|
|
18
|
+
pip install -e '.[test]'
|
|
19
|
+
pytest
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
## One term, three ends
|
|
23
|
+
|
|
24
|
+
An *interaction net* is a graph of nodes, called agents. Each agent has one *principal
|
|
25
|
+
port*, through which it interacts, and some auxiliary ports. Where a wire joins two
|
|
26
|
+
principal ports there is an *active pair*, and a rule replaces the pair by a small net.
|
|
27
|
+
That is all a net can do.
|
|
28
|
+
|
|
29
|
+
The rules do not say which active pair to fire first. For rules that look at nothing but
|
|
30
|
+
their pair, the order does not matter to the result: every order that stops, stops at the
|
|
31
|
+
same net after the same number of interactions. That is a theorem about interaction nets,
|
|
32
|
+
which this package relies on and does not prove. What the order does decide is *whether*
|
|
33
|
+
the net stops. `inets demo` shows it on one term of the combinators S, K and I: `K I Ω`,
|
|
34
|
+
where `Ω` reduces to itself without end and `K` discards it.
|
|
35
|
+
|
|
36
|
+
```
|
|
37
|
+
ski table, by need: only the pairs the answer depends on
|
|
38
|
+
answer I; 2 interactions, of which 1 contractions; stopped because: wanted; active pairs left: 2
|
|
39
|
+
ski table, every pair fired: Ω runs although K will discard it
|
|
40
|
+
answer none: stopped at the cap of 300 interactions; 301 interactions, of which 149 contractions; stopped because: interactions; active pairs left: 3
|
|
41
|
+
lazy table, every pair fired: the demand is carried in the net, and no work is shared
|
|
42
|
+
answer I; 18 interactions, of which 1 contractions; stopped because: normal; active pairs left: 0
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
Every run says why it stopped. `normal` means no active pair is left. `wanted` means
|
|
46
|
+
reduction by need has what it was asked for and has left other pairs alone: what it reaches
|
|
47
|
+
is a normal form of the part of the net the answer can reach, not of the net.
|
|
48
|
+
|
|
49
|
+
## A system is a table
|
|
50
|
+
|
|
51
|
+
This is a whole table, for the symmetric interaction combinators. It comes with the
|
|
52
|
+
package as `combinators`.
|
|
53
|
+
|
|
54
|
+
```
|
|
55
|
+
agent con 2 # a constructor: two auxiliary ports
|
|
56
|
+
agent dup 2 # a duplicator
|
|
57
|
+
agent era 0 # an eraser
|
|
58
|
+
|
|
59
|
+
rule con (a b) >< con (c d) # two of a kind annihilate
|
|
60
|
+
=> a = c; b = d
|
|
61
|
+
count annihilate
|
|
62
|
+
|
|
63
|
+
rule dup (a b) >< dup (c d)
|
|
64
|
+
=> a = c; b = d
|
|
65
|
+
count annihilate
|
|
66
|
+
|
|
67
|
+
rule con (a b) >< dup (c d) # two of different kinds copy each other
|
|
68
|
+
=> a = dup (p q); b = dup (r s); c = con (p r); d = con (q s)
|
|
69
|
+
count commute
|
|
70
|
+
|
|
71
|
+
rule era >< con (a b) # an eraser erases, and sends erasers on
|
|
72
|
+
=> a = era; b = era
|
|
73
|
+
count erase
|
|
74
|
+
|
|
75
|
+
rule era >< dup (a b)
|
|
76
|
+
=> a = era; b = era
|
|
77
|
+
count erase
|
|
78
|
+
|
|
79
|
+
rule era >< era
|
|
80
|
+
=> nothing
|
|
81
|
+
count erase
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
In the right-hand side of a rule a letter bound on the left is the port that was joined
|
|
85
|
+
there, any other letter is a new wire and occurs exactly twice, and `dup (p q)` is a new
|
|
86
|
+
node standing for its principal port. `count` names the statistic the rule adds to.
|
|
87
|
+
|
|
88
|
+
A table may also give each port a sign, `+` for an output and `-` for an input, as the
|
|
89
|
+
tables for S, K and I do:
|
|
90
|
+
|
|
91
|
+
```
|
|
92
|
+
agent S2 + - - # S x y: an output, and two inputs for x and y
|
|
93
|
+
agent app - - + # application: (function) (argument result)
|
|
94
|
+
|
|
95
|
+
rule app (z r) >< S2 (x y)
|
|
96
|
+
=> z = dup (p q); x = app (p f); y = app (q g); f = app (g r)
|
|
97
|
+
count contract
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
The full format, with conditions, references to named nets and calls to a host, is in the
|
|
101
|
+
docstring of [`inets.system`](src/inets/system.py).
|
|
102
|
+
|
|
103
|
+
```python
|
|
104
|
+
import inets
|
|
105
|
+
|
|
106
|
+
system = inets.bundled("combinators") # or inets.System.load("a-table-of-yours.rules")
|
|
107
|
+
print(system.check()) # [] if no problem was found
|
|
108
|
+
|
|
109
|
+
net = inets.Net(system)
|
|
110
|
+
a, b = net.free(), net.free() # two free ports: loose ends to attach to or read from
|
|
111
|
+
net.wire(net.make("con", a, b), net.make("dup", net.make("era"), net.make("era")))
|
|
112
|
+
result = net.reduce() # or net.reduce_in_rounds(), or net.reduce_by_need(port)
|
|
113
|
+
print(result.reason, result.counts) # normal {'commute': 1, 'erase': 2}
|
|
114
|
+
print(net.dot()) # the net as Graphviz text
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
`net.make(agent, *ports, param=value)` makes a node, joins its auxiliary ports to the ports
|
|
118
|
+
given, and returns its principal port; `net.wire` joins two ports. A port is a node's
|
|
119
|
+
number and an index, 0 for the principal port. To read a result, follow a free port:
|
|
120
|
+
`net.link[port]` is the port it is joined to, and `net.kind[node]` and `net.param[node]`
|
|
121
|
+
say what is there. A table whose rules expand named nets or call functions needs a host:
|
|
122
|
+
`inets.Net(system, inets.Library({name: build}))` supplies named nets, and a subclass of
|
|
123
|
+
`inets.Host` supplies functions.
|
|
124
|
+
|
|
125
|
+
## What checking a table means
|
|
126
|
+
|
|
127
|
+
`system.check()`, or `inets check TABLE`, reports each problem as a sentence. For any
|
|
128
|
+
table:
|
|
129
|
+
|
|
130
|
+
- a rule with no body or no count, or one that uses a parameter or a port it does not
|
|
131
|
+
have, gives an agent a parameter it does not take, or names two ports with one letter;
|
|
132
|
+
- a rule whose right-hand side does not use every auxiliary port of its pair exactly once,
|
|
133
|
+
or leaves a port of a new node unjoined: the net it makes must have the pair's free
|
|
134
|
+
ports;
|
|
135
|
+
- a rule that can never apply, because an earlier one for the same pair always does;
|
|
136
|
+
- a pair whose rules have conditions that cannot be seen to cover every case (end with
|
|
137
|
+
`otherwise`);
|
|
138
|
+
- a test of a neighbour on a port or of an agent that does not exist, and an `expand` of
|
|
139
|
+
an agent that has auxiliary ports.
|
|
140
|
+
|
|
141
|
+
For a table without signs, also: a pair of agents with no rule.
|
|
142
|
+
|
|
143
|
+
For a table with signs, also: an agent without them; a rule not written consumer `><`
|
|
144
|
+
value (a consumer's principal port is an input, a value's an output); a consumer and value
|
|
145
|
+
with no rule, or a rule between two values or two consumers; and a rule that joins two
|
|
146
|
+
outputs or two inputs. If no rule does that, a net whose wires each join an output to an
|
|
147
|
+
input stays that way, so the only principal ports that can meet are a consumer's and a
|
|
148
|
+
value's, and each such pair has a rule.
|
|
149
|
+
|
|
150
|
+
It does not decide whether a system terminates, and it does not show that a rule is the
|
|
151
|
+
one intended: S with its arguments exchanged passes every check. `inets rules TABLE` marks
|
|
152
|
+
each rule that is *not* a local rule: one that looks at a neighbour, expands a named net,
|
|
153
|
+
calls the host, or keeps one of its pair. The theorem about order is for local rules.
|
|
154
|
+
|
|
155
|
+
## Three schedules
|
|
156
|
+
|
|
157
|
+
| Method | Fires | Needs | Stops with |
|
|
158
|
+
|---|---|---|---|
|
|
159
|
+
| `net.reduce(order=...)` | one active pair at a time: `"stack"` (newest first), `"queue"` (oldest first) or `"random"` | any table | `normal` |
|
|
160
|
+
| `net.reduce_in_rounds()` | every active pair, round by round | any table | `normal`, with the number of rounds and the widest round |
|
|
161
|
+
| `net.reduce_by_need(port)` | only the pairs that the value wanted at `port` depends on | a table with signs | `wanted`, with the active pairs it left |
|
|
162
|
+
|
|
163
|
+
Each takes `max_interactions` and stops with `interactions` if it gets there; a run in
|
|
164
|
+
rounds looks at the cap between rounds, so it may pass it by less than a round. The number
|
|
165
|
+
of rounds is the net's span: the longest chain of interactions that must follow one
|
|
166
|
+
another. A result's counts are for that run. Any schedule raises `inets.Stuck` at a pair
|
|
167
|
+
no rule applies to; reduction by need raises it too if the value wanted depends on a free
|
|
168
|
+
port.
|
|
169
|
+
|
|
170
|
+
Reduction by need never fires the pairs that would erase what is no longer wanted. So that
|
|
171
|
+
a long run does not fill up, it sweeps the net when it passes 20,000 nodes, and again each
|
|
172
|
+
time the net reaches twice the size the last sweep left (`sweep_above` sets the figure):
|
|
173
|
+
`net.sweep(ports)` keeps what those ports can reach and removes the rest. Sweeping needs a
|
|
174
|
+
table with exactly one eraser, an agent whose only port is an input; without one,
|
|
175
|
+
`net.sweep` raises an error and reduction by need goes on unswept. `deep=False` stops
|
|
176
|
+
reduction by need as soon as the value wanted is a value, its own inputs not asked for.
|
|
177
|
+
|
|
178
|
+
## The tables that come with it
|
|
179
|
+
|
|
180
|
+
| `inets.bundled(...)` | What | Agents, rules |
|
|
181
|
+
|---|---|---|
|
|
182
|
+
| `"combinators"` | The symmetric interaction combinators: a constructor, a duplicator, an eraser | 3, 6 |
|
|
183
|
+
| `"ski"` | The combinators S, K and I as agents, with signs. An application fires as soon as its function is a value | 12, 27 |
|
|
184
|
+
| `"lazy"` | The same with the demand for evaluation carried by agents in the net, so that every pair can be fired and only wanted work is done; no work is shared | 15, 50 |
|
|
185
|
+
| `"ivm"` | The interaction system of the IVM, the machine that runs [Vine](https://github.com/VineLang/vine), read from its source | 5, 20 |
|
|
186
|
+
|
|
187
|
+
[`inets.ski`](src/inets/ski.py) reads and writes terms of S, K and I, builds them as nets,
|
|
188
|
+
names their subterms as references to nets, and reduces them in the three ways of the demo
|
|
189
|
+
(`inets run ski TERM`, with `--rounds` or `--lazy`, and `--dot` to print the net that is
|
|
190
|
+
left as Graphviz text):
|
|
191
|
+
|
|
192
|
+
```python
|
|
193
|
+
from inets import ski
|
|
194
|
+
run = ski.run("```SII0") # S I I 0, by need
|
|
195
|
+
ski.show(run.term), run.result.counts # ('`00', {'collect': 3, 'contract': 3, 'copy': 1})
|
|
196
|
+
```
|
|
197
|
+
|
|
198
|
+
[`inets.ivm`](src/inets/ivm.py) runs programs written in Ivy, Vine's text for nets, by the
|
|
199
|
+
`ivm` table (`inets run ivm FILE.iv`). For the thirteen programs in `tests/ivm`, the tests
|
|
200
|
+
hold it, in three orders of reduction, to the output, the six interaction counts and the
|
|
201
|
+
error flags that `ivm run` gave when they were recorded; with `IVM` set they compare
|
|
202
|
+
output and counts with the binary directly. Six programs compiled from Vine's own
|
|
203
|
+
examples, and the IVM's example `hi.iv`, agreed in the same way in a run that is not kept
|
|
204
|
+
here and cannot be repeated from this repository. It supplies twenty extrinsic functions (N32 arithmetic and comparison, the IVM's
|
|
205
|
+
branch, `print_char`, and the IO value's split and merge) and stops at any other.
|
|
206
|
+
|
|
207
|
+
[`inets.ivy`](src/inets/ivy.py) reads Ivy text for any system.
|
|
208
|
+
|
|
209
|
+
`inets --help` lists the commands.
|
|
210
|
+
|
|
211
|
+
## In a notebook
|
|
212
|
+
|
|
213
|
+
[`kernel/`](kernel) is a second small package, `inets-kernel`: a Jupyter kernel in which a
|
|
214
|
+
cell is a table, a net, or a command. A net is written as the right-hand side of a rule
|
|
215
|
+
is, and is drawn before and after it is reduced, with the active pairs marked.
|
|
216
|
+
|
|
217
|
+
```sh
|
|
218
|
+
pip install inets-kernel
|
|
219
|
+
python -m inets_kernel.install --sys-prefix # or --user
|
|
220
|
+
jupyter lab notebooks/
|
|
221
|
+
```
|
|
222
|
+
|
|
223
|
+
[`notebooks/`](notebooks) has three to start from: the three rules of the combinators;
|
|
224
|
+
building a system of your own, for adding numbers; and one term reaching three ends. They
|
|
225
|
+
are written and run by `notebooks/make.py`, so what they show is what the kernel gives.
|
|
226
|
+
The pictures need Graphviz's `dot`.
|
|
227
|
+
|
|
228
|
+
## Tests
|
|
229
|
+
|
|
230
|
+
```sh
|
|
231
|
+
pytest # about ten seconds; 13 tests are skipped unless IVM is set
|
|
232
|
+
IVM=/path/to/ivm pytest # also compares with an ivm binary directly
|
|
233
|
+
```
|
|
234
|
+
|
|
235
|
+
The tests cover the checks on the bundled tables and on deliberately broken ones; small
|
|
236
|
+
nets of the combinators, with one net reduced in every order; the Ivy reader; the command
|
|
237
|
+
line; the IVM programs against counts recorded from `ivm run` (VineLang/vine at commit
|
|
238
|
+
`cf0c325`); and a census of S, K and I. The census takes every term up to size 5 (size 4
|
|
239
|
+
with two atoms) that has a normal form by ordinary term rewriting within its caps. By need
|
|
240
|
+
and by the `lazy` table each reaches that normal form, the `lazy` table in exactly as many
|
|
241
|
+
contractions as rewriting takes steps and by need in no more. With every pair fired, the
|
|
242
|
+
`ski` table reaches it or runs into the cap.
|
|
243
|
+
|
|
244
|
+
## What it is not
|
|
245
|
+
|
|
246
|
+
- **Fast.** A net is Python dictionaries: roughly a hundred thousand interactions a
|
|
247
|
+
second, measured by hand on one laptop.
|
|
248
|
+
- **A language.** There is a table format and a Python interface, and nothing above them.
|
|
249
|
+
- **A proof.** Checks and tests.
|
|
250
|
+
- **Complete for the IVM.** Memory, depth, workers and most extrinsic functions are left
|
|
251
|
+
out.
|
|
252
|
+
|
|
253
|
+
## Where it comes from
|
|
254
|
+
|
|
255
|
+
The tables, the checks and the machine were worked out in
|
|
256
|
+
[vine-aviary](https://github.com/sigilante/vine-aviary), which has the measurements behind
|
|
257
|
+
the `ski` and `lazy` tables and the argument for why a net of agents cannot both stop when
|
|
258
|
+
it should and share work. This package is that code made general. It does not yet replace
|
|
259
|
+
it: vine-aviary's own machine also removes a duplicator that has lost one of its copies,
|
|
260
|
+
where this one gives the lost copy to an eraser, so its counts of copies under reduction
|
|
261
|
+
by need should be lower than this package's (the two have not been compared).
|
|
262
|
+
|
|
263
|
+
## Licence
|
|
264
|
+
|
|
265
|
+
MIT. See [LICENSE](LICENSE).
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=77"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "inets"
|
|
7
|
+
version = "0.1.0"
|
|
8
|
+
description = "Interaction nets: declare a system as a table of rules, check it, and reduce nets by it"
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
requires-python = ">=3.10"
|
|
11
|
+
license = "MIT"
|
|
12
|
+
license-files = ["LICENSE"]
|
|
13
|
+
authors = [{ name = "N. E. Davis" }]
|
|
14
|
+
keywords = ["interaction nets", "interaction combinators", "combinatory logic", "graph rewriting", "Vine", "IVM"]
|
|
15
|
+
classifiers = [
|
|
16
|
+
"Development Status :: 3 - Alpha",
|
|
17
|
+
"Intended Audience :: Education",
|
|
18
|
+
"Intended Audience :: Science/Research",
|
|
19
|
+
"Programming Language :: Python :: 3",
|
|
20
|
+
"Topic :: Scientific/Engineering",
|
|
21
|
+
"Topic :: Education",
|
|
22
|
+
]
|
|
23
|
+
dependencies = []
|
|
24
|
+
|
|
25
|
+
[project.optional-dependencies]
|
|
26
|
+
test = ["pytest"]
|
|
27
|
+
|
|
28
|
+
[project.urls]
|
|
29
|
+
Repository = "https://github.com/sigilante/inets"
|
|
30
|
+
|
|
31
|
+
[project.scripts]
|
|
32
|
+
inets = "inets.__main__:main"
|
|
33
|
+
|
|
34
|
+
[tool.setuptools.packages.find]
|
|
35
|
+
where = ["src"]
|
|
36
|
+
|
|
37
|
+
[tool.setuptools.package-data]
|
|
38
|
+
"inets.systems" = ["*.rules"]
|
|
39
|
+
|
|
40
|
+
[tool.pytest.ini_options]
|
|
41
|
+
testpaths = ["tests"]
|
|
42
|
+
pythonpath = ["src"]
|
inets-0.1.0/setup.cfg
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
"""Interaction nets: declare a system as a table of rules, check it, and reduce nets by it.
|
|
2
|
+
|
|
3
|
+
>>> import inets
|
|
4
|
+
>>> system = inets.bundled("combinators")
|
|
5
|
+
>>> system.check()
|
|
6
|
+
[]
|
|
7
|
+
>>> net = inets.Net(system)
|
|
8
|
+
>>> a, b = net.free(), net.free()
|
|
9
|
+
>>> net.wire(net.make("con", a, b), net.make("dup", net.make("era"), net.make("era")))
|
|
10
|
+
>>> result = net.reduce()
|
|
11
|
+
>>> result.reason, result.counts
|
|
12
|
+
('normal', {'commute': 1, 'erase': 2})
|
|
13
|
+
|
|
14
|
+
See :mod:`inets.system` for the table format, :mod:`inets.net` for nets and schedules,
|
|
15
|
+
:mod:`inets.ski` for the combinators S, K and I as agents, :mod:`inets.ivm` for programs
|
|
16
|
+
of Vine's IVM, and :mod:`inets.ivy` for Ivy text.
|
|
17
|
+
"""
|
|
18
|
+
from .errors import Error, IvyError, Stuck, TableError, TermError
|
|
19
|
+
from .net import Host, Library, Net, Port, Result
|
|
20
|
+
from .system import Agent, Rule, System, bundled
|
|
21
|
+
|
|
22
|
+
__version__ = "0.1.0"
|
|
23
|
+
|
|
24
|
+
__all__ = ["System", "Agent", "Rule", "bundled", "Net", "Port", "Result", "Host", "Library",
|
|
25
|
+
"Error", "TableError", "IvyError", "TermError", "Stuck"]
|