0.0.1 • Published 3 years ago

tree-term-rewriting v0.0.1

Weekly downloads
-
License
MIT
Repository
-
Last release
3 years ago

Tree Term Rewriting

This is a work in progress library written in TypeScript for term rewriting using trees. The library is still a work in progress, things need to be more organized, more tests need to be written and so on.

Features

Term Rewriting on Trees

The functions applyRules() and rewrite() can be used to rewrite a tree given a rewrite rule containing a pattern and a replacement.

Example (the expressions would be represented as trees in the library):

  • Input: a + b + -b
  • Rewrite rule from: $x + -$x
  • Rewrite rule to: 0
  • Output: a + 0

Expression parsing

The library provides a function to parse expressions into trees. The grammar is contained in the grammars folder.

There are two representations for trees. The first one is the obvious one with nodes (type TreeNode) where each node stores its own value, a unique index as well as a list of child nodes. The second one stores the tree using an Euler Tour. This representation makes tree matching and substitution easier. There are functions to convert between the two representations (makeEulerTree(), treeFromEulerTree()).

Knuth-Bendix Completion

Knuth-Bendix completion finds a confluent term rewriting system given a couple of basic rewrite rules and an ordering on functions. Basically it will find more rewrite rules such that applying the rules to equal but different terms will eventually result in the same term (eg. a + b - b and a * 3 / 3 are the same, and eventually would simplify to a).

Visualization / DOT

There are functions to convert trees to the DOT format which can be visualized by many different softwares. There are also functions to directly convert them to urls that visualize them with using https://dreampuf.github.io/GraphvizOnline.

Building

The library can be built by running yarn run build which will create the dist folder containing the javascript code, the definitions and the source map.

Run tests

  • Run all tests (unit tests and integration tests): yarn run test
  • Run only specific tests: yarn run test -g <string|regexp> (eg. to run integration tests yarn run test -g integration)

Examples

The examples can be run by first building the library and then running them in the dist folder (eg. node dist/examples/ga.js).

References

These are references that I found useful for learning about tree rewriting and Knuth-Bendix completion