Metadata-Version: 2.4
Name: skilark-joy
Version: 0.11.0
Summary: Joy on SKIjack: a concatenative language whose words are closed SKI supercombinators
Author: N. E. Davis
License-Expression: MIT
Project-URL: Repository, https://github.com/sigilante/SKIlark
Keywords: concatenative,Joy,Forth,SKI,combinator,skijack
Classifier: Development Status :: 2 - Pre-Alpha
Classifier: Intended Audience :: Science/Research
Classifier: Programming Language :: Python :: 3
Classifier: Topic :: Software Development :: Interpreters
Requires-Python: >=3.10
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: skijack<0.3,>=0.2.6
Provides-Extra: test
Requires-Dist: pytest>=7; extra == "test"
Requires-Dist: pytest-xdist>=3; extra == "test"
Dynamic: license-file

# SKIlark

![](./img/hero.jpeg)

Joy on SKIjack. A concatenative language in the line of Joy (von Thun) and
Kerby's *Theory of Concatenative Combinators*, written as
[SKIjack](https://github.com/sigilante/SKIjack) programs: its words are
closed SKI supercombinators, its programs are Scott lists of words or the
compositions of those words, and its dictionary is the program's own
namespace.

- `skilark/programs/kernel.ascii.ski` — the kernel: Joy's basis `dup swap
  pop cons cat unit dip i` plus literals, quotations and `+`, and Joy's
  next words: `- * / rem = <` on signed, unbounded integers, the truth
  values `true false` with `branch` and `ifte`, quotations as lists,
  `uncons null small`, a word in one a symbol (`[dup +] first` is `dup`),
  the stack as data, `stack unstack`, characters and strings (`'a`,
  `"ab"`, `ord chr`), sets of 0 to 63 (`{1 2}`), Joy's `typeof` and `not`
  (a set's complement). `succ pred max min neg abs sign and or > <= >=
  != xor first rest swons unswons concat size at of take drop has in`
  are derived, and so are the type tests `integer char logical set
  string list leaf user float file sametype`, Joy's combinators,
  `nullary infra times while tailrec linrec binrec genrec primrec map
  filter step fold`, and its library words (`unary2 cleave construct
  cond case split some all equal treerec` and their kin, PLAN.md §3,
  stage 7), and `div pick condlinrec condnestrec casting`, with `compare`
  a kernel word (stage 9), `name body intern` (stage 10; `intern` in
  the interpreted form alone), `strtol format` (stage 11), and the I/O
  words `put putch putchars get argc argv getenv clock abort quit`
  (stage 14, the interpreted form's), which stop the run for the host
  to answer (`skilark/io.py`); run by a
  fold over the program (`exec`, one unit of
  fuel per word, the fuel the Church numeral 10,000; `execU`, no fuel).
  SKIlark grows toward Joy in full, in stages (`PLAN.md` §3), and Joy's
  behaviour is the reference implementation's: `tests/joyref.py` models
  it, and the tests check every pinned outcome against the model, but
  for four deviations pinned with their reason (`genrec`'s R2 and
  `treerec`'s and `treegenrec`'s C see a quotation, not the reference's
  own word; and after an error SKIlark runs on the stack the erring term
  began with, where the reference keeps the stack as the error left it,
  `PLAN.md` §3, stage 14). The reference's own test suite
  (`tests/joy/`, stage 16) runs too, with the limits it found pinned: no
  recursive definitions, literals at most 900 either way, and three
  `casting` pointer reinterpretations left out. `word` has 30 of the 31
  constructors Avon's cap of 32 arguments allows (`PLAN.md` §3, stage 6); the last,
  `Ext ext`, holds the kernel's further words, `size at compare name
  body intern strtol format` and the I/O words, in a sum of their own
  (§3, stage 8).
  `basis.ascii.ski` is the basis alone, with
  SKIlark's own `ifz`, as phase 3 measured it.
- `skilark/programs/joy.{ascii,unicode}.ski` — the kernel with a
  hand-written set of programs and Joy's laws (phase 1).
- `skilark/joy.py` — the front end. Joy text to SKIjack source in three
  forms: *interpreted* (a `prog` datum under the kernel), *compiled*
  (every word a function, a program the composition of its words,
  quotations defunctionalized, no interpreter present), and *level 1*
  (either form under SKIjack's blocking interpreter `wfN`, with
  definitions in a namespace read by scry).
- `skilark/io.py` — the host of the I/O words. An I/O word ends a run
  as `RIO`, holding the word, the stack and the rest of the program; the
  host does what the word does and runs the rest. `Host` answers from a
  given world (input, arguments, environment, clock), as the tests do;
  `live()` from this process's, as `python -m skilark` does:

      $ echo '"joy"' | python -m skilark '"name? " putchars get put'
      name? "joy"
- `skilark/door.py` — the I/O words through drivers (stage 15). A door
  makes a program or a Joy file a kernel whose I/O words are drivers'
  cards (write, read, environment, step count, exit), and a runtime with
  drivers performs them: SKIjack's reference drivers, or `avon run
  --kernel`. `put`'s writer and `get`'s reader are SKI, in the door.
  `python -m skilark.door` writes a file's door for Avon:

      $ python -m skilark.door examples/hello.joy > joy
      $ avon run --kernel -- joy
      5
      "hello, world"
      [1 4 9]
- `skilark/zk.py` — predicates in zero knowledge: a Joy program as the
  verifier of a predicate over literal bits, which lean-ski's composed
  circuit proves. `split` adds two hidden bit strings and compares the sum
  with `x`; `python -m skilark.zk 8` writes its SKIjack source, which
  lean-ski compiles and proves. The pipeline is sound; the statement is
  weak, not yet a range proof (`PLAN.md` §3 says why). `csplit` adds a
  knapsack commitment to `a` (`python -m skilark.zk csplit 8 8`): the
  shape of a committed range proof, at toy sizes.
- `skilark/variants.py` — phase 3's measurements: the kernel with its
  loop (the splice, a continuation stack, results sequenced) or its fuel
  (computed, literal, Church) replaced, on the phase-2 programs
  (`python -m skilark.variants`). Church fuel cut a fuelled run to a
  quarter and is now the kernel's; the splice stays (`PLAN.md` §3).
- `PLAN.md` — what is built, what was measured, and what comes next.
- `BENCHMARKS.md` — stage 6's costs and savings, step by step, in
  contractions, on Avon and in CI.

The distribution is `skilark-joy`, since `skilark` on PyPI is another
project's; it installs the package `skilark`.

```
pip install -e ".[test]"
python -m pytest -q -n auto      # a test a core; the level-1 measurements need --runslow
python -m skilark "3 [dup +] i"
python -m skilark --define "sq == dup +" "1 2 + sq"
python -m skilark "5 fact"                # by ifte and Kerby's y
python -m skilark "-7 2 / -7 2 rem"       # -3 -1, as C truncates
python -m skilark "[1 2 3 4] 2 take size" # 2
python -m skilark "[1 2 3] [dup *] map"   # [1 4 9]
python -m skilark "5 [1] [*] primrec"     # 120
python -m skilark "{1 2} {2 3} xor"       # {1 3}
python -m skilark '"abc" [succ] map'      # "bcd"
```

```
$ python -m skilark "3 [dup +] i"
interpreted  unfuelled      5,056  6
interpreted  fuelled        5,052  6
compiled     unfuelled        428  6
compiled     fuelled          442  6
```

Every reduction runs through SKIjack, by default in Python on
`aviary-kernel`'s algorithm.  [Avon](https://github.com/sigilante/avon), a
C runtime, runs the same reductions: build it (`make` in an Avon checkout),
then

```
export AVON=/path/to/avon/build/avon
python -m skilark --reducer avon  "3 [dup +] i"    # reduced by Avon
python -m skilark --reducer check "3 [dup +] i"    # by both, compared
SKIJACK_REDUCER=avon python -m pytest -q -n auto --runslow  # the suite, on Avon
```

Avon runs in its copy mode, the reference host's algorithm, so every
contraction count above, and every one the tests pin, is the same on both.
The reducer switch is SKIjack's (`SKIJACK_REDUCER`), from skijack 0.2.1;
SKIlark requires skijack 0.2.6. CI runs the suite on Avon on each Python. On each
push to master, and on a pull request labelled `reference`, it also runs the
suite with every reduction checked against aviary (`SKIJACK_REDUCER=check`).

**Examples.** `examples/` holds whole Joy programs, each beside the
output the reference prints (`NAME.out`): `hello`, `recursion`
(`linrec`, `binrec`, `primrec`), `lists`, `qsort`, `gcd` (`tailrec`,
`while`), `control` (`cond`, `times`), `text` (`format`, `strtol`),
`sets`, `symbols` (`name`, `body`) and `intern`. Run one as the reference
runs a file, each term on the stack the terms before it left and the top
printed after each:

```sh
python -m skilark examples/qsort.joy        # [1 2 3]  and  []
```

**The SKI underneath.** `--ski` shows a program as the closed term over
S, K and I alone that it runs as, with its size and what it reduces to,
and `--ski-out FILE.skit` (or `.dag`, shared) writes it for Avon:

```sh
python -m skilark --ski --form compiled "1 2 +"
# compiled: 644 atoms as a tree, 386 nodes shared
# K (S (S (K S) ...
# reduces in 227 contractions to 3
python -m skilark --ski-out p.skit --form compiled "1 2 +" && avon reduce --whnf p.skit
```

An interpreted program carries the kernel's `step`, about 75,000 atoms;
the compiled form carries only the words it uses.

`skilark.joyfile` reads the reference's file syntax, `(* *)` and `#`
comments, `DEFINE` blocks and terms ended by `.`; `tests/test_examples.py`
checks every example against the model and, in the slow job, SKIlark
against both.

```python
>>> from skilark import Joy, compiled
>>> Joy().run("r7")                                   # 3 sumto, by Kerby's y
(('RVal', [6]), 29641)
>>> compiled({"p": "5 sumto"}).run("up")
(('RVal', [15]), 27453)
```
