tiny-datalog 0.6.0__tar.gz → 0.7.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.
- {tiny_datalog-0.6.0/tiny_datalog.egg-info → tiny_datalog-0.7.0}/PKG-INFO +36 -20
- {tiny_datalog-0.6.0 → tiny_datalog-0.7.0}/README.md +35 -19
- {tiny_datalog-0.6.0 → tiny_datalog-0.7.0}/pyproject.toml +2 -1
- {tiny_datalog-0.6.0 → tiny_datalog-0.7.0}/tiny_datalog/__init__.py +4 -2
- {tiny_datalog-0.6.0 → tiny_datalog-0.7.0}/tiny_datalog/core.py +3 -2
- {tiny_datalog-0.6.0 → tiny_datalog-0.7.0}/tiny_datalog/datalog.py +33 -19
- {tiny_datalog-0.6.0 → tiny_datalog-0.7.0}/tiny_datalog/engines.py +35 -7
- tiny_datalog-0.7.0/tiny_datalog/slg.py +553 -0
- {tiny_datalog-0.6.0 → tiny_datalog-0.7.0}/tiny_datalog/tabling.py +140 -46
- {tiny_datalog-0.6.0 → tiny_datalog-0.7.0/tiny_datalog.egg-info}/PKG-INFO +36 -20
- {tiny_datalog-0.6.0 → tiny_datalog-0.7.0}/tiny_datalog.egg-info/SOURCES.txt +1 -0
- {tiny_datalog-0.6.0 → tiny_datalog-0.7.0}/tiny_datalog.egg-info/entry_points.txt +1 -0
- {tiny_datalog-0.6.0 → tiny_datalog-0.7.0}/LICENSE +0 -0
- {tiny_datalog-0.6.0 → tiny_datalog-0.7.0}/setup.cfg +0 -0
- {tiny_datalog-0.6.0 → tiny_datalog-0.7.0}/tiny_datalog/chase.py +0 -0
- {tiny_datalog-0.6.0 → tiny_datalog-0.7.0}/tiny_datalog/containment.py +0 -0
- {tiny_datalog-0.6.0 → tiny_datalog-0.7.0}/tiny_datalog/defeasible.py +0 -0
- {tiny_datalog-0.6.0 → tiny_datalog-0.7.0}/tiny_datalog/incremental.py +0 -0
- {tiny_datalog-0.6.0 → tiny_datalog-0.7.0}/tiny_datalog/magic.py +0 -0
- {tiny_datalog-0.6.0 → tiny_datalog-0.7.0}/tiny_datalog/prolog.py +0 -0
- {tiny_datalog-0.6.0 → tiny_datalog-0.7.0}/tiny_datalog/semantics.py +0 -0
- {tiny_datalog-0.6.0 → tiny_datalog-0.7.0}/tiny_datalog/semiring.py +0 -0
- {tiny_datalog-0.6.0 → tiny_datalog-0.7.0}/tiny_datalog/subsumption.py +0 -0
- {tiny_datalog-0.6.0 → tiny_datalog-0.7.0}/tiny_datalog.egg-info/dependency_links.txt +0 -0
- {tiny_datalog-0.6.0 → tiny_datalog-0.7.0}/tiny_datalog.egg-info/top_level.txt +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: tiny-datalog
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.7.0
|
|
4
4
|
Summary: A Datalog engine whose core you can read in an afternoon, and a course built on it
|
|
5
5
|
Author: Andrew Goodchild
|
|
6
6
|
License-Expression: MIT
|
|
@@ -127,7 +127,7 @@ Nothing to install:
|
|
|
127
127
|
|
|
128
128
|
```sh
|
|
129
129
|
git clone https://github.com/andrewgoodchild/tiny-datalog && cd tiny-datalog
|
|
130
|
-
python3 tests.py #
|
|
130
|
+
python3 tests.py # 402 tests, ~20s
|
|
131
131
|
```
|
|
132
132
|
|
|
133
133
|
## Why the language choice decides what you can ask later
|
|
@@ -229,20 +229,29 @@ is the whole relation the guards are pure overhead.
|
|
|
229
229
|
[Lesson 7](https://github.com/andrewgoodchild/tiny-datalog/blob/main/lessons/07-magic-sets.md) works through why.
|
|
230
230
|
|
|
231
231
|
Correctness is checked by a seeded differential fuzzer that generates
|
|
232
|
-
stratified programs and demands
|
|
233
|
-
|
|
234
|
-
|
|
232
|
+
stratified programs, aggregates included, and demands that all five
|
|
233
|
+
engines agree; that SLG matches the well-founded model on unstratified
|
|
234
|
+
ones, with and without subsumptive tabling; and that incremental
|
|
235
|
+
maintenance matches recomputation under random updates. 400 programs
|
|
236
|
+
per run;
|
|
235
237
|
`TINY_DATALOG_FUZZ=3000 python3 tests.py DifferentialFuzzTests` soaks.
|
|
236
238
|
|
|
237
239
|
Answers are also checked against engines this repository did not write.
|
|
238
240
|
[`conformance/`](https://github.com/andrewgoodchild/tiny-datalog/tree/main/conformance)
|
|
239
241
|
runs the [datalog-conformance](https://pypi.org/project/datalog-conformance/)
|
|
240
|
-
corpus, harvested from Soufflé, Nemo and Crepe, through all
|
|
242
|
+
corpus, harvested from Soufflé, Nemo and Crepe, through all five
|
|
241
243
|
strategies, and its defeasible theories, from SPINdle and the papers,
|
|
242
244
|
through `defeasible.py`: 195 cases pass. Every skip is named with its
|
|
243
245
|
reason: arithmetic and 10⁵-fact joins, which this course omits on
|
|
244
246
|
purpose, or cases written for a different logic.
|
|
245
247
|
|
|
248
|
+
And the tests themselves are checked: `benchmarks/mutate.py` makes
|
|
249
|
+
each one-line break a bug could make (a `<` for a `<=`, a dropped
|
|
250
|
+
`not`, a deleted statement) and runs the suite against each. At 0.7.0
|
|
251
|
+
the suite caught 91% of 3,322 such mutants. Most of the rest change
|
|
252
|
+
nothing a user could observe, such as a counter's starting value or a
|
|
253
|
+
default limit raised by one; the others are mainly CLI printing.
|
|
254
|
+
|
|
246
255
|
## Learning Datalog
|
|
247
256
|
|
|
248
257
|
`lessons/` is a complete course, no prior exposure assumed, every
|
|
@@ -380,7 +389,7 @@ trusting, state properties as constraints); copy it into your agent's
|
|
|
380
389
|
skills directory.
|
|
381
390
|
|
|
382
391
|
The command-line interface installs too, as `tiny-datalog` (and
|
|
383
|
-
`tiny-datalog-semiring`, `-tabling`, `-incremental`, `-subsumption`,
|
|
392
|
+
`tiny-datalog-semiring`, `-tabling`, `-slg`, `-incremental`, `-subsumption`,
|
|
384
393
|
`-containment`, `-defeasible`, `-prolog` for the other engines and the
|
|
385
394
|
extensions):
|
|
386
395
|
|
|
@@ -398,15 +407,18 @@ tiny_datalog/ the code you read, in three tiers:
|
|
|
398
407
|
core.py the common core — the language every module shares:
|
|
399
408
|
AST, parser, safety checks, comparisons, stratification,
|
|
400
409
|
matching, formatting, query helpers, warnings
|
|
401
|
-
engines —
|
|
410
|
+
engines — five strategies, one answer (engines.py lists them):
|
|
402
411
|
datalog.py the base engine: semi-naive and naive bottom-up
|
|
403
412
|
evaluation, aggregation, integrity constraints,
|
|
404
413
|
--explain / why-not, --assume, the CLI
|
|
405
414
|
magic.py the magic-sets rewriting (a program-to-program pass)
|
|
406
415
|
tabling.py tabled top-down evaluation (iterative QSQR)
|
|
407
|
-
engines.py the registry and the interface the
|
|
416
|
+
engines.py the registry and the interface the five share (the
|
|
417
|
+
fifth is SLG, from slg.py below, held to stratified
|
|
418
|
+
programs)
|
|
408
419
|
extensions — other questions asked of the same programs:
|
|
409
420
|
semantics.py grounding, stable models, the well-founded model
|
|
421
|
+
slg.py SLG resolution: the well-founded model, top-down
|
|
410
422
|
semiring.py semiring-valued evaluation (costs, counts, provenance,
|
|
411
423
|
probabilities)
|
|
412
424
|
incremental.py insertions + DRed deletions over a live materialisation
|
|
@@ -427,8 +439,10 @@ skills/ an Agent Skill that teaches coding agents to use the engine
|
|
|
427
439
|
conformance/ the external datalog-conformance corpus (Soufflé, Nemo,
|
|
428
440
|
Crepe, SPINdle), run against every evaluation strategy
|
|
429
441
|
benchmarks/ scaled input generators (chain/tree/clique/grid, an EL
|
|
430
|
-
ontology, and the N-queens puzzle)
|
|
431
|
-
|
|
442
|
+
ontology, and the N-queens puzzle), and mutate.py,
|
|
443
|
+
which breaks the code one line at a time to measure
|
|
444
|
+
how much of it the tests would notice (91% at 0.7.0)
|
|
445
|
+
tests.py 402 tests: every shipped program and exercise answer is
|
|
432
446
|
executed, a conformance suite runs every query through
|
|
433
447
|
every engine that handles it, and a seeded fuzzer checks
|
|
434
448
|
the same property on random programs
|
|
@@ -444,7 +458,7 @@ used.
|
|
|
444
458
|
This project started as an attempt at a tiny implementation of
|
|
445
459
|
Datalog, small enough to read in an afternoon. It grew with the
|
|
446
460
|
course. Each lesson that taught a new technique — magic sets, stable models,
|
|
447
|
-
semirings, incremental maintenance, tabling, subsumption, containment,
|
|
461
|
+
semirings, incremental maintenance, tabling, SLG resolution, subsumption, containment,
|
|
448
462
|
defeasible logic, the chase — brought a module to run it, and the repository is
|
|
449
463
|
now several thousand lines. The core idea stayed small, and the code
|
|
450
464
|
is laid out so you can still read just that: the common core and the
|
|
@@ -455,21 +469,23 @@ Measured in lines, comments, docstrings and blank lines included:
|
|
|
455
469
|
|
|
456
470
|
| Tier | Lines |
|
|
457
471
|
|---|---|
|
|
458
|
-
| the common core (`core.py`: the language, evaluating nothing; about 270 of these lines arrived with comparisons, constraints, value checks and the warnings) | 1,
|
|
472
|
+
| the common core (`core.py`: the language, evaluating nothing; about 270 of these lines arrived with comparisons, constraints, value checks and the warnings) | 1,057 |
|
|
459
473
|
| the base engine's evaluator (`datalog.py` up to its CLI section: the module docstring, the import list that re-exports the core, `Program`, `Engine`, aggregation folding, `run_program`) | 423 |
|
|
460
|
-
| `--explain`, why-not, constraint reports, `--assume`, printing and the CLI (the rest of `datalog.py`) |
|
|
461
|
-
| the other pluggable engines: magic sets (`magic.py`) and tabling (`tabling.py`), plus the `engines.py` registry (naive evaluation is a flag on the base engine) |
|
|
462
|
-
| the
|
|
474
|
+
| `--explain`, why-not, constraint reports, `--assume`, printing and the CLI (the rest of `datalog.py`) | 635 |
|
|
475
|
+
| the other pluggable engines: magic sets (`magic.py`) and tabling (`tabling.py`), plus the `engines.py` registry (naive evaluation is a flag on the base engine; the fifth engine, SLG, is counted with the extensions) | 700 |
|
|
476
|
+
| the nine extensions | 3,413 |
|
|
463
477
|
|
|
464
478
|
So the smallest complete Datalog system here — parse, check, stratify,
|
|
465
479
|
evaluate semi-naively — is the core plus the evaluator, about 1,500
|
|
466
|
-
lines. All of it is
|
|
480
|
+
lines. All of it is 6.2k lines of toolkit (every module but the
|
|
467
481
|
re-export list in `__init__.py`), roughly a quarter of it commentary,
|
|
468
|
-
and
|
|
482
|
+
and 4.4k of tests.
|
|
469
483
|
|
|
470
484
|
"Tiny" is a claim about that core and evaluator, and about each module
|
|
471
|
-
built on them singly: none of the
|
|
472
|
-
`engines.py` and the
|
|
485
|
+
built on them singly: none of the twelve (`magic.py`, `tabling.py`,
|
|
486
|
+
`engines.py` and the nine extensions) exceeds 500 lines, except
|
|
487
|
+
`slg.py`, allowed 600 because it is tabling plus a scheduler, and a
|
|
488
|
+
test asserts both limits. It is not a
|
|
473
489
|
claim about the repository, which is a dozen modules because it
|
|
474
490
|
teaches a dozen things.
|
|
475
491
|
|
|
@@ -94,7 +94,7 @@ Nothing to install:
|
|
|
94
94
|
|
|
95
95
|
```sh
|
|
96
96
|
git clone https://github.com/andrewgoodchild/tiny-datalog && cd tiny-datalog
|
|
97
|
-
python3 tests.py #
|
|
97
|
+
python3 tests.py # 402 tests, ~20s
|
|
98
98
|
```
|
|
99
99
|
|
|
100
100
|
## Why the language choice decides what you can ask later
|
|
@@ -196,20 +196,29 @@ is the whole relation the guards are pure overhead.
|
|
|
196
196
|
[Lesson 7](https://github.com/andrewgoodchild/tiny-datalog/blob/main/lessons/07-magic-sets.md) works through why.
|
|
197
197
|
|
|
198
198
|
Correctness is checked by a seeded differential fuzzer that generates
|
|
199
|
-
stratified programs and demands
|
|
200
|
-
|
|
201
|
-
|
|
199
|
+
stratified programs, aggregates included, and demands that all five
|
|
200
|
+
engines agree; that SLG matches the well-founded model on unstratified
|
|
201
|
+
ones, with and without subsumptive tabling; and that incremental
|
|
202
|
+
maintenance matches recomputation under random updates. 400 programs
|
|
203
|
+
per run;
|
|
202
204
|
`TINY_DATALOG_FUZZ=3000 python3 tests.py DifferentialFuzzTests` soaks.
|
|
203
205
|
|
|
204
206
|
Answers are also checked against engines this repository did not write.
|
|
205
207
|
[`conformance/`](https://github.com/andrewgoodchild/tiny-datalog/tree/main/conformance)
|
|
206
208
|
runs the [datalog-conformance](https://pypi.org/project/datalog-conformance/)
|
|
207
|
-
corpus, harvested from Soufflé, Nemo and Crepe, through all
|
|
209
|
+
corpus, harvested from Soufflé, Nemo and Crepe, through all five
|
|
208
210
|
strategies, and its defeasible theories, from SPINdle and the papers,
|
|
209
211
|
through `defeasible.py`: 195 cases pass. Every skip is named with its
|
|
210
212
|
reason: arithmetic and 10⁵-fact joins, which this course omits on
|
|
211
213
|
purpose, or cases written for a different logic.
|
|
212
214
|
|
|
215
|
+
And the tests themselves are checked: `benchmarks/mutate.py` makes
|
|
216
|
+
each one-line break a bug could make (a `<` for a `<=`, a dropped
|
|
217
|
+
`not`, a deleted statement) and runs the suite against each. At 0.7.0
|
|
218
|
+
the suite caught 91% of 3,322 such mutants. Most of the rest change
|
|
219
|
+
nothing a user could observe, such as a counter's starting value or a
|
|
220
|
+
default limit raised by one; the others are mainly CLI printing.
|
|
221
|
+
|
|
213
222
|
## Learning Datalog
|
|
214
223
|
|
|
215
224
|
`lessons/` is a complete course, no prior exposure assumed, every
|
|
@@ -347,7 +356,7 @@ trusting, state properties as constraints); copy it into your agent's
|
|
|
347
356
|
skills directory.
|
|
348
357
|
|
|
349
358
|
The command-line interface installs too, as `tiny-datalog` (and
|
|
350
|
-
`tiny-datalog-semiring`, `-tabling`, `-incremental`, `-subsumption`,
|
|
359
|
+
`tiny-datalog-semiring`, `-tabling`, `-slg`, `-incremental`, `-subsumption`,
|
|
351
360
|
`-containment`, `-defeasible`, `-prolog` for the other engines and the
|
|
352
361
|
extensions):
|
|
353
362
|
|
|
@@ -365,15 +374,18 @@ tiny_datalog/ the code you read, in three tiers:
|
|
|
365
374
|
core.py the common core — the language every module shares:
|
|
366
375
|
AST, parser, safety checks, comparisons, stratification,
|
|
367
376
|
matching, formatting, query helpers, warnings
|
|
368
|
-
engines —
|
|
377
|
+
engines — five strategies, one answer (engines.py lists them):
|
|
369
378
|
datalog.py the base engine: semi-naive and naive bottom-up
|
|
370
379
|
evaluation, aggregation, integrity constraints,
|
|
371
380
|
--explain / why-not, --assume, the CLI
|
|
372
381
|
magic.py the magic-sets rewriting (a program-to-program pass)
|
|
373
382
|
tabling.py tabled top-down evaluation (iterative QSQR)
|
|
374
|
-
engines.py the registry and the interface the
|
|
383
|
+
engines.py the registry and the interface the five share (the
|
|
384
|
+
fifth is SLG, from slg.py below, held to stratified
|
|
385
|
+
programs)
|
|
375
386
|
extensions — other questions asked of the same programs:
|
|
376
387
|
semantics.py grounding, stable models, the well-founded model
|
|
388
|
+
slg.py SLG resolution: the well-founded model, top-down
|
|
377
389
|
semiring.py semiring-valued evaluation (costs, counts, provenance,
|
|
378
390
|
probabilities)
|
|
379
391
|
incremental.py insertions + DRed deletions over a live materialisation
|
|
@@ -394,8 +406,10 @@ skills/ an Agent Skill that teaches coding agents to use the engine
|
|
|
394
406
|
conformance/ the external datalog-conformance corpus (Soufflé, Nemo,
|
|
395
407
|
Crepe, SPINdle), run against every evaluation strategy
|
|
396
408
|
benchmarks/ scaled input generators (chain/tree/clique/grid, an EL
|
|
397
|
-
ontology, and the N-queens puzzle)
|
|
398
|
-
|
|
409
|
+
ontology, and the N-queens puzzle), and mutate.py,
|
|
410
|
+
which breaks the code one line at a time to measure
|
|
411
|
+
how much of it the tests would notice (91% at 0.7.0)
|
|
412
|
+
tests.py 402 tests: every shipped program and exercise answer is
|
|
399
413
|
executed, a conformance suite runs every query through
|
|
400
414
|
every engine that handles it, and a seeded fuzzer checks
|
|
401
415
|
the same property on random programs
|
|
@@ -411,7 +425,7 @@ used.
|
|
|
411
425
|
This project started as an attempt at a tiny implementation of
|
|
412
426
|
Datalog, small enough to read in an afternoon. It grew with the
|
|
413
427
|
course. Each lesson that taught a new technique — magic sets, stable models,
|
|
414
|
-
semirings, incremental maintenance, tabling, subsumption, containment,
|
|
428
|
+
semirings, incremental maintenance, tabling, SLG resolution, subsumption, containment,
|
|
415
429
|
defeasible logic, the chase — brought a module to run it, and the repository is
|
|
416
430
|
now several thousand lines. The core idea stayed small, and the code
|
|
417
431
|
is laid out so you can still read just that: the common core and the
|
|
@@ -422,21 +436,23 @@ Measured in lines, comments, docstrings and blank lines included:
|
|
|
422
436
|
|
|
423
437
|
| Tier | Lines |
|
|
424
438
|
|---|---|
|
|
425
|
-
| the common core (`core.py`: the language, evaluating nothing; about 270 of these lines arrived with comparisons, constraints, value checks and the warnings) | 1,
|
|
439
|
+
| the common core (`core.py`: the language, evaluating nothing; about 270 of these lines arrived with comparisons, constraints, value checks and the warnings) | 1,057 |
|
|
426
440
|
| the base engine's evaluator (`datalog.py` up to its CLI section: the module docstring, the import list that re-exports the core, `Program`, `Engine`, aggregation folding, `run_program`) | 423 |
|
|
427
|
-
| `--explain`, why-not, constraint reports, `--assume`, printing and the CLI (the rest of `datalog.py`) |
|
|
428
|
-
| the other pluggable engines: magic sets (`magic.py`) and tabling (`tabling.py`), plus the `engines.py` registry (naive evaluation is a flag on the base engine) |
|
|
429
|
-
| the
|
|
441
|
+
| `--explain`, why-not, constraint reports, `--assume`, printing and the CLI (the rest of `datalog.py`) | 635 |
|
|
442
|
+
| the other pluggable engines: magic sets (`magic.py`) and tabling (`tabling.py`), plus the `engines.py` registry (naive evaluation is a flag on the base engine; the fifth engine, SLG, is counted with the extensions) | 700 |
|
|
443
|
+
| the nine extensions | 3,413 |
|
|
430
444
|
|
|
431
445
|
So the smallest complete Datalog system here — parse, check, stratify,
|
|
432
446
|
evaluate semi-naively — is the core plus the evaluator, about 1,500
|
|
433
|
-
lines. All of it is
|
|
447
|
+
lines. All of it is 6.2k lines of toolkit (every module but the
|
|
434
448
|
re-export list in `__init__.py`), roughly a quarter of it commentary,
|
|
435
|
-
and
|
|
449
|
+
and 4.4k of tests.
|
|
436
450
|
|
|
437
451
|
"Tiny" is a claim about that core and evaluator, and about each module
|
|
438
|
-
built on them singly: none of the
|
|
439
|
-
`engines.py` and the
|
|
452
|
+
built on them singly: none of the twelve (`magic.py`, `tabling.py`,
|
|
453
|
+
`engines.py` and the nine extensions) exceeds 500 lines, except
|
|
454
|
+
`slg.py`, allowed 600 because it is tabling plus a scheduler, and a
|
|
455
|
+
test asserts both limits. It is not a
|
|
440
456
|
claim about the repository, which is a dozen modules because it
|
|
441
457
|
teaches a dozen things.
|
|
442
458
|
|
|
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
|
|
|
4
4
|
|
|
5
5
|
[project]
|
|
6
6
|
name = "tiny-datalog"
|
|
7
|
-
version = "0.
|
|
7
|
+
version = "0.7.0"
|
|
8
8
|
description = "A Datalog engine whose core you can read in an afternoon, and a course built on it"
|
|
9
9
|
readme = "README.md"
|
|
10
10
|
license = "MIT"
|
|
@@ -48,6 +48,7 @@ tiny-datalog = "tiny_datalog.datalog:main"
|
|
|
48
48
|
tiny-datalog-prolog = "tiny_datalog.prolog:main"
|
|
49
49
|
tiny-datalog-semiring = "tiny_datalog.semiring:main"
|
|
50
50
|
tiny-datalog-tabling = "tiny_datalog.tabling:main"
|
|
51
|
+
tiny-datalog-slg = "tiny_datalog.slg:main"
|
|
51
52
|
tiny-datalog-incremental = "tiny_datalog.incremental:main"
|
|
52
53
|
tiny-datalog-subsumption = "tiny_datalog.subsumption:main"
|
|
53
54
|
tiny-datalog-containment = "tiny_datalog.containment:main"
|
|
@@ -9,10 +9,12 @@ are their public surface, re-exported here so that library use reads as
|
|
|
9
9
|
|
|
10
10
|
`tiny_datalog.engines` lists the pluggable engines, which all give the
|
|
11
11
|
same answers: semi-naive and naive (`datalog`), `magic` (goal-directed
|
|
12
|
-
rewriting)
|
|
12
|
+
rewriting), `tabling` (top-down with memoing) and `slg` (SLG
|
|
13
|
+
resolution, held to stratified programs).
|
|
13
14
|
|
|
14
15
|
Everything else is an extension built on the core, asking a different
|
|
15
16
|
question of a program: `semantics` (stable and well-founded models),
|
|
17
|
+
`slg` (the well-founded model top-down, by SLG resolution),
|
|
16
18
|
`semiring` (provenance-weighted evaluation), `incremental` (maintenance
|
|
17
19
|
under updates), `subsumption` (KL-ONE style classification),
|
|
18
20
|
`containment` (query containment), `defeasible` (rules with exceptions
|
|
@@ -36,7 +38,7 @@ from tiny_datalog.datalog import (
|
|
|
36
38
|
format_atom, format_fact,
|
|
37
39
|
)
|
|
38
40
|
|
|
39
|
-
__version__ = "0.
|
|
41
|
+
__version__ = "0.7.0"
|
|
40
42
|
|
|
41
43
|
__all__ = [
|
|
42
44
|
"Var", "Const", "Struct", "Atom", "Literal", "Rule",
|
|
@@ -10,8 +10,9 @@ The package is built in three tiers:
|
|
|
10
10
|
what a Datalog program *is*, and evaluates nothing.
|
|
11
11
|
* the **engines**: interchangeable evaluation strategies that give the
|
|
12
12
|
same answers to the same stratified program — semi-naive and naive
|
|
13
|
-
bottom-up (datalog.py, the base engine), magic sets (magic.py)
|
|
14
|
-
tabling (tabling.py)
|
|
13
|
+
bottom-up (datalog.py, the base engine), magic sets (magic.py),
|
|
14
|
+
tabling (tabling.py) and SLG resolution (slg.py, also an
|
|
15
|
+
extension). engines.py names them and states their shared
|
|
15
16
|
interface.
|
|
16
17
|
* the **extensions**: modules that ask a *different* question of a
|
|
17
18
|
program, and so give different answers on purpose — stable and
|
|
@@ -865,22 +865,29 @@ def _run_magic_query(q, clauses, trace):
|
|
|
865
865
|
suffix=" [magic]")
|
|
866
866
|
|
|
867
867
|
|
|
868
|
-
def _run_tabled_queries(queries, clauses, trace):
|
|
869
|
-
"""-q under --engine tabling, through the engines.py
|
|
870
|
-
answers print exactly as every other engine's do;
|
|
871
|
-
size of the work, and tabling.py --tables
|
|
868
|
+
def _run_tabled_queries(queries, clauses, trace, name="tabling"):
|
|
869
|
+
"""-q under --engine tabling or slg, through the engines.py
|
|
870
|
+
interface. The answers print exactly as every other engine's do;
|
|
871
|
+
--trace adds the size of the work, and tabling.py --tables (or
|
|
872
|
+
slg.py --tables) lists the tables."""
|
|
872
873
|
from tiny_datalog.engines import ENGINES
|
|
873
|
-
tabled = ENGINES[
|
|
874
|
+
tabled = ENGINES[name](clauses)
|
|
874
875
|
for q in queries:
|
|
875
876
|
atom = parse_query_atom(q, tabled.engine.arity)
|
|
876
|
-
_print_answers(atom, tabled.answers(atom),
|
|
877
|
+
_print_answers(atom, tabled.answers(atom),
|
|
878
|
+
suffix=" [tabled]" if name == "tabling"
|
|
879
|
+
else " [slg]")
|
|
877
880
|
if trace:
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
881
|
+
n = len(tabled.engine.tables)
|
|
882
|
+
if name == "tabling":
|
|
883
|
+
work = "filled in %d round%s" % (
|
|
884
|
+
tabled.engine.rounds,
|
|
885
|
+
"" if tabled.engine.rounds == 1 else "s")
|
|
886
|
+
else:
|
|
887
|
+
done = tabled.engine.stats["completions"]
|
|
888
|
+
work = "%d completion%s" % (done, "" if done == 1 else "s")
|
|
889
|
+
print("[%s] %d subgoal table%s, %s (%s.py --tables lists them)"
|
|
890
|
+
% (name, n, "" if n == 1 else "s", work, name))
|
|
884
891
|
print()
|
|
885
892
|
|
|
886
893
|
|
|
@@ -888,7 +895,7 @@ def _run_tabled_queries(queries, clauses, trace):
|
|
|
888
895
|
# imported: engines.py imports this module, so this module can only
|
|
889
896
|
# import it lazily, inside a function — and a test pins the two lists
|
|
890
897
|
# together.
|
|
891
|
-
ENGINE_NAMES = ["seminaive", "naive", "magic", "tabling"]
|
|
898
|
+
ENGINE_NAMES = ["seminaive", "naive", "magic", "tabling", "slg"]
|
|
892
899
|
|
|
893
900
|
|
|
894
901
|
def _engine_choice(args):
|
|
@@ -941,11 +948,11 @@ def main(argv=None):
|
|
|
941
948
|
"'edge(a, z). edge(a, b)~.' (repeatable)")
|
|
942
949
|
ap.add_argument("--engine", choices=ENGINE_NAMES, default=None,
|
|
943
950
|
help="evaluation strategy (default seminaive; see "
|
|
944
|
-
"engines.py). All
|
|
951
|
+
"engines.py). All five give the same answers. "
|
|
945
952
|
"seminaive and naive materialise every relation, "
|
|
946
953
|
"so they also print whole relations and support "
|
|
947
|
-
"--explain; magic and
|
|
948
|
-
"they answer -q queries only and refuse "
|
|
954
|
+
"--explain; magic, tabling and slg are "
|
|
955
|
+
"goal-directed: they answer -q queries only and refuse "
|
|
949
956
|
"--explain, which reads a derivation tree off a "
|
|
950
957
|
"full materialisation. --trace shows each "
|
|
951
958
|
"engine's own work: rounds and deltas, the "
|
|
@@ -967,7 +974,8 @@ def main(argv=None):
|
|
|
967
974
|
print(warning, file=sys.stderr)
|
|
968
975
|
|
|
969
976
|
rules, constraints = split_constraints(clauses)
|
|
970
|
-
|
|
977
|
+
goal_directed = engine_name in ("magic", "tabling", "slg")
|
|
978
|
+
if constraints and (args.models or goal_directed):
|
|
971
979
|
# constraints change no answer, so these modes may set them
|
|
972
980
|
# aside -- but say so, rather than imply they were checked
|
|
973
981
|
print("note: %d integrity constraint%s not checked: that needs "
|
|
@@ -979,7 +987,7 @@ def main(argv=None):
|
|
|
979
987
|
if args.models:
|
|
980
988
|
return _print_models(clauses)
|
|
981
989
|
|
|
982
|
-
if
|
|
990
|
+
if goal_directed:
|
|
983
991
|
flag = "--magic" if args.magic else "--engine %s" % engine_name
|
|
984
992
|
if not args.query:
|
|
985
993
|
print("error: %s requires at least one -q/--query" % flag,
|
|
@@ -996,9 +1004,15 @@ def main(argv=None):
|
|
|
996
1004
|
for q in args.query:
|
|
997
1005
|
_run_magic_query(q, clauses, args.trace)
|
|
998
1006
|
else:
|
|
999
|
-
_run_tabled_queries(args.query, clauses, args.trace
|
|
1007
|
+
_run_tabled_queries(args.query, clauses, args.trace,
|
|
1008
|
+
engine_name)
|
|
1000
1009
|
except StratificationError as exc:
|
|
1001
1010
|
print("REJECTED: %s" % exc, file=sys.stderr)
|
|
1011
|
+
if engine_name == "slg":
|
|
1012
|
+
print("(--engine slg keeps to stratified programs, as every "
|
|
1013
|
+
"engine does. slg.py runs this one: true and "
|
|
1014
|
+
"undefined answers, the well-founded model.)",
|
|
1015
|
+
file=sys.stderr)
|
|
1002
1016
|
return 2
|
|
1003
1017
|
return 0
|
|
1004
1018
|
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
engines.py — the pluggable evaluation strategies, behind one interface.
|
|
3
3
|
|
|
4
4
|
The package has three tiers (core.py's docstring describes them). This
|
|
5
|
-
file is the middle one made explicit:
|
|
5
|
+
file is the middle one made explicit: five algorithms that answer the
|
|
6
6
|
*same* question — what follows from a stratified program? — and must
|
|
7
7
|
therefore give identical answers. They differ only in how much work
|
|
8
8
|
they do and in which order:
|
|
@@ -15,6 +15,8 @@ they do and in which order:
|
|
|
15
15
|
bottom-up (magic.py — Lesson 7)
|
|
16
16
|
tabling top-down with a table of answers per subgoal
|
|
17
17
|
(tabling.py — Lesson 15)
|
|
18
|
+
slg top-down with suspension and completion: SLG
|
|
19
|
+
resolution (slg.py — Lesson 15)
|
|
18
20
|
|
|
19
21
|
The interface, which every entry of ENGINES follows:
|
|
20
22
|
|
|
@@ -32,8 +34,16 @@ aside; checking them takes the full materialisation (Engine.violations).
|
|
|
32
34
|
|
|
33
35
|
Every engine requires a *stratified* program — that is what makes their
|
|
34
36
|
answers comparable — and refuses another one with StratificationError.
|
|
35
|
-
`handles` is for the narrower, per-engine gaps
|
|
36
|
-
|
|
37
|
+
`handles` is for the narrower, per-engine gaps. There are none at
|
|
38
|
+
present: tabling and slg once said False to an aggregate rule, until
|
|
39
|
+
both learned to table aggregation. The method stays, for the next
|
|
40
|
+
engine with a gap.
|
|
41
|
+
|
|
42
|
+
SLG is the one engine that could run more: it computes the
|
|
43
|
+
well-founded model, so it has answers for unstratified programs too.
|
|
44
|
+
Here it is held to the registry's contract, where that model has no
|
|
45
|
+
undefined atoms and equals the stratified one; slg.py's own CLI runs
|
|
46
|
+
the rest.
|
|
37
47
|
|
|
38
48
|
Because the answers must agree, the registry doubles as a test oracle:
|
|
39
49
|
tests.py (ConformanceTests, DifferentialFuzzTests) and
|
|
@@ -50,10 +60,10 @@ not their contract.
|
|
|
50
60
|
|
|
51
61
|
from __future__ import annotations
|
|
52
62
|
|
|
53
|
-
from tiny_datalog.core import
|
|
54
|
-
aggregate_of, split_constraints, stratify, validate)
|
|
63
|
+
from tiny_datalog.core import split_constraints, stratify, validate
|
|
55
64
|
from tiny_datalog.datalog import Engine, Program
|
|
56
65
|
from tiny_datalog.magic import magic_query
|
|
66
|
+
from tiny_datalog.slg import SLGEngine
|
|
57
67
|
from tiny_datalog.tabling import TabledEngine
|
|
58
68
|
|
|
59
69
|
|
|
@@ -108,11 +118,11 @@ class Magic:
|
|
|
108
118
|
|
|
109
119
|
class Tabling:
|
|
110
120
|
"""Tabled top-down evaluation (iterative QSQR): one table of answers
|
|
111
|
-
per subgoal, grown to fixpoint.
|
|
121
|
+
per subgoal, grown to fixpoint."""
|
|
112
122
|
|
|
113
123
|
@staticmethod
|
|
114
124
|
def handles(clauses):
|
|
115
|
-
return
|
|
125
|
+
return True
|
|
116
126
|
|
|
117
127
|
def __init__(self, clauses):
|
|
118
128
|
self.engine = TabledEngine(split_constraints(clauses)[0])
|
|
@@ -121,6 +131,23 @@ class Tabling:
|
|
|
121
131
|
return self.engine.query(atom)
|
|
122
132
|
|
|
123
133
|
|
|
134
|
+
class SLG(Tabling):
|
|
135
|
+
"""SLG resolution: tabling that suspends a call on an incomplete
|
|
136
|
+
table instead of re-running it, and completes groups of tables off
|
|
137
|
+
a stack. On a stratified program every answer is true."""
|
|
138
|
+
|
|
139
|
+
def __init__(self, clauses):
|
|
140
|
+
# SLGEngine would run an unstratified program; the registry
|
|
141
|
+
# promises to refuse one, as every other engine does
|
|
142
|
+
clauses = split_constraints(clauses)[0]
|
|
143
|
+
stratify(clauses)
|
|
144
|
+
self.engine = SLGEngine(clauses)
|
|
145
|
+
|
|
146
|
+
def answers(self, atom):
|
|
147
|
+
true, _undefined = self.engine.query(atom)
|
|
148
|
+
return true
|
|
149
|
+
|
|
150
|
+
|
|
124
151
|
# name -> engine class; insertion order is the order tests and the CLI
|
|
125
152
|
# list them, reference first
|
|
126
153
|
ENGINES = {
|
|
@@ -128,4 +155,5 @@ ENGINES = {
|
|
|
128
155
|
"naive": Naive,
|
|
129
156
|
"magic": Magic,
|
|
130
157
|
"tabling": Tabling,
|
|
158
|
+
"slg": SLG,
|
|
131
159
|
}
|