Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
name: CI

on:
push:
branches: [main]
pull_request:
branches: [main]

permissions:
contents: read

jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v7
- uses: fabasoad/setup-prolog-action@v1
- name: Rewrite test
run: test/hash_consing_rewrite.sh | diff - test/hash_consing_rewrite_baseline.txt
- name: The pack installs and loads
run: |
mkdir -p "$RUNNER_TEMP/packs"
swipl -g "pack_install('.', [pack_directory('$RUNNER_TEMP/packs'), interactive(false), register(false)]), use_module(library(hash_consing))" -t halt

version:
if: github.event_name == 'pull_request'
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v7
with:
fetch-depth: 0
- uses: fabasoad/setup-prolog-action@v1
- name: pack.pl names a version above every release
run: |
VERSION="$(swipl -g "read_file_to_terms('pack.pl', Terms, []), memberchk(version(Version), Terms), write(Version)" -t halt)"
test -n "$VERSION"
if git rev-parse --quiet --verify "refs/tags/V$VERSION" > /dev/null; then
echo "V$VERSION is already released; raise version/1 in pack.pl" >&2
exit 1
fi
LATEST="$(git tag --list 'V*' | sed 's/^V//' | sort -V | tail -n 1)"
if [ -n "$LATEST" ]; then
test "$(printf '%s\n%s\n' "$LATEST" "$VERSION" | sort -V | tail -n 1)" = "$VERSION"
fi

publish:
needs: test
if: github.event_name == 'push' && github.ref == 'refs/heads/main'
runs-on: ubuntu-latest
permissions:
contents: write
steps:
- uses: actions/checkout@v7
with:
fetch-depth: 0
- uses: fabasoad/setup-prolog-action@v1
- name: swipl pack publish
run: |
git config user.name "github-actions[bot]"
git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
git branch --set-upstream-to=origin/main main
swipl -g "pack_publish('.', [sign(false), branch(main), interactive(false)])" -t halt
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# pack_install('.') links the pack directory here and writes its bookkeeping file into it.
/status.db
59 changes: 57 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,57 @@
# interning
Hash-consed terms for SWI-Prolog, and a load-time clause rewrite that makes a program use them
# hash_consing

[![CI](https://github.com/Atry/hash_consing/actions/workflows/ci.yml/badge.svg)](https://github.com/Atry/hash_consing/actions/workflows/ci.yml)

Hash-consed terms for SWI-Prolog, and a load-time rewrite that makes a
program use them. A file is written as the program that does not intern and
names the constructors to intern; every clause read after that directive
refers to their instances by Ids, each wrapping a node handle of one trie,
and the store is canonical: the same term has the same Id.

## Install

```prolog
?- pack_install(hash_consing).
```

## Use

```prolog
:- module(steps, [step/2, built/2]).
:- use_module(library(hash_consing), []).
:- hash_consing:rewritten([apply(*, _), lambda(_), variable(_)]).

step(apply(lambda(Body), Argument), beta(Body, Argument)).

built(Function, Applied) :-
Applied = apply(Function, variable(0)).
```

A term that did not come through the rewrite crosses the boundary with
`hash_consing:internalized/3`, and `hash_consing:externalized/2` turns Ids back
into terms. The module comment of [`prolog/hash_consing.pl`](prolog/hash_consing.pl)
is the reference: the relation `intern/2`, the Id and its shape, the
templates, the rewrite of a clause, the two rules for a file that opts in,
and the known defects.

## Test

```sh
test/hash_consing_rewrite.sh | diff - test/hash_consing_rewrite_baseline.txt
```

prints nothing when the rewrite and `intern/2` behave as specified.

## Contributing and releases

Open pull requests against `main`. `main` changes only by merging a pull
request with a merge commit, and every merge into `main` is a release:
CI runs `swipl pack publish` on the merged `main`, which tags `V<version>`
from the `version/1` of `pack.pl`, installs the pack from this repository
in an isolated directory and registers it with the SWI-Prolog pack server.
A pull request into `main` must therefore raise `version/1` above every
released version; a required check enforces it.

## License

MIT. See [`LICENSE`](LICENSE).
7 changes: 7 additions & 0 deletions pack.pl
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
name(hash_consing).
version('0.1.0').
title('Hash-consed terms by clause rewriting').
keywords([interning, 'hash-consing', trie, term_expansion]).
author('Yang, Bo', 'yang-bo@yang-bo.com').
home('https://github.com/Atry/hash_consing').
download('https://github.com/Atry/hash_consing.git').
Loading
Loading