• Stars
    star
    103
  • Rank 331,107 (Top 7 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 9 years ago
  • Updated 11 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Compile and run Constraint Handling Rules (CHR) in JavaScript

CHR.js

Compile and run Constraint Handling Rules (CHR) in JavaScript.

CHR.js is a just-in-time (JIT) compiler for Constraint Handling Rules, embedded in JavaScript. For better runtime performance it supports ahead-of-time (AOT) compilation too, either by its command line tool chrjs or babel-plugin-chr, a plugin for Babel.

Getting Started

The online version at chrjs.net is the easiest way to generate a constraint solver. Just enter your Constraint Handling Rules, try adding some constraints, and download the generated solver code.

Example

The following CHR rule generates all fibonacci numbers upto a given index Max as constraints of the form fib(Number,Value).

upto(Max), fib(A,AV), fib(B,BV) ==> B === A+1, B < Max | fib(B+1,AV+BV)

The CHR rule can be used in JavaScript after declaring it via the chr() function, like in this example:

var CHR = require('chr')             // load the module
var chr = CHR()                      // create new solver
    
// add the rule
chr('upto(Max), fib(A,AV), fib(B,BV) ==> \
       B === A+1, B < Max | fib(B+1,AV+BV)')

console.log(chr.Store.toString())    // print the content of the
                                     //   constraint store
/* results in:
    (empty)
*/

Promise.all([
  chr.fib(1,1),                      // the first Fibonacci is 1
  chr.fib(2,1)                       // the second is 1
]).then(function () {
  console.log(chr.Store.toString())  // both have been stored
  /* results in:
      ID  Constraint
      --  ----------
      1   fib(1,1)  
      2   fib(2,1)  
  */

  // now generate the Fibonaccis upto the 5th element
  chr.upto(5).then(function () {
    console.log(chr.Store.toString())
  })
  /* results in:
      ID  Constraint
      --  ----------
      1   fib(1,1)  
      2   fib(2,1)  
      3   upto(5)   
      4   fib(3,2)  
      5   fib(4,3)  
      6   fib(5,5)
  */
})

More example CHR scripts are provided at chrjs.net.

Defining CHR rules in this way, they are compiled at runtime, that means we use a just-in-time (JIT) compilation. However, for performance reasons, we encourage the use of an ahead-of-time (AOT) compiler as presented in the next section.

AOT Compilation

CHR.js comes with a CLI to pre-compile CHR programs:

$ cat example.chr
gcd(0) <=> true
gcd(N) \ gcd(M) <=> 0 < N, N <= M | gcd(M - N)
$ chrjs example.chr > example.js
$ node
> var chr = require('./example.js')
> chr.gcd(1000).then(function () { console.log(chr.Store.toString()) })
> chr.gcd(42).then(function () { console.log(chr.Store.toString()) })

Functions encapsulated in ${ ... } are evaluated at rule application, as for JIT compilation too.

In addition to the traditional compilation mode, CHR.js can create an optimized compiled version using only syncronous functions and constraints, resulting in a way better performance of the generated constraint solver. Use the --optimized flag:

$ chrjs --optimized example.chr

REPL

CHR.js provides a REPL (Read-eval-print loop) to use it interactively with the command line. The CHR > REPL can be started by calling node repl.js from within the project's root directory. Then it is possible to directly define rules and call constraints:

CHR > dec(0) <=> true
  [Rule] Added.
CHR > dec(N) ==> dec(N-1)
  [Rule] Added.
CHR > dec(4)
  ID  Constraint
  --  ----------
  1   dec(4)    
  2   dec(3)    
  3   dec(2)    
  4   dec(1)

The REPL can also be used programmatically by calling var Repl = require('chr/repl').

Background

CHR.js was realized as a part of my Master Thesis in Computer Science at the University of Ulm, Germany. Its Project Report for a prototype implementation (versions 0.x) with additional information about its architecture can be found online: https://fnogatz.github.io/paper-now-chrjs/.

The implementation is based on the compilation scheme presented in the paper CHR for imperative host languages (2008; Peter Van Weert, Pieter Wuille, Tom Schrijvers, Bart Demoen). As of yet basically none of the mentioned optimizations have been implemented.

More Repositories

1

clocker

Command-line tool to track project hours
JavaScript
420
star
2

xsd2json

Translate XML Schema into equivalent JSON Schema
Prolog
150
star
3

magento2-matomo

Matomo Analytics Module for Magento 2
PHP
50
star
4

swivm

SWI-Prolog Version Manager - Bash script to manage multiple active SWI-Prolog versions
Shell
41
star
5

dgtchess

An event-driven JavaScript connector for electronic DGT chess boards
JavaScript
23
star
6

geospatial-demo

Example location-based app to demonstrate the power of MongoDB, node.js and Websockets
JavaScript
17
star
7

transportation

node.js module to display and manipulate public transport data
JavaScript
16
star
8

plammar

A Prolog grammar written in Prolog, for parsing and serialising Prolog code.
Prolog
12
star
9

tap

Write TAP tests with SWI-Prolog
Prolog
12
star
10

dcg4pt

Extend Definite Clause Grammars for Prolog by the corresponding parse tree
Prolog
9
star
11

feedback-to-gitlab

Server to generate GitLab issues for user feedback
JavaScript
9
star
12

timetraveller

Webservice to explore spatio-temporal data in an interactive way.
JavaScript
7
star
13

talks

A little collection of talks I gave
JavaScript
7
star
14

CHR.js-website

The code that runs http://chrjs.net
JavaScript
7
star
15

date_time

Logical arithmetic on dates and times in Prolog
Prolog
6
star
16

race

Prolog client for the SOAP interface of the Attempto Reasoner RACE
Prolog
6
star
17

CHR-Constraint-Server

A Persistent CHR Webserver
Prolog
5
star
18

CHR-Minesweeper

Simple Minesweeper application with Prolog and Constraint Handling Rules (CHR)
Prolog
5
star
19

CHR-Linear-Equation-Solver

Linear Equation Solving using Constraint Handling Rules
Prolog
4
star
20

cli_table

Pretty unicode tables for the CLI with Prolog
Prolog
4
star
21

RKI-Data-Diff-lfs

Shell scripts to get a minimal set of SQL commands for persistent yet efficient storage of RKI data for COVID19
Shell
4
star
22

Thesis-Makefile

Curated Makefile with best practices, conventions, and tips for writing LaTeX theses.
Makefile
3
star
23

CHR-Benchmarks

Compare the execution runtimes of different implementations of Constraint Handling Rules (CHR)
C
3
star
24

babel-plugin-chr

Babel plugin that precompiles CHR.js source code
JavaScript
3
star
25

CTX

Chess Tournament Exchange Format
Shell
3
star
26

tablediff

Shell scripts to get a minimal set of SQL commands for table synchronisation.
Shell
2
star
27

alexa.pl

Alexa skill development with SWI-Prolog
Prolog
2
star
28

chess.json

JSON Schema for saving Chess Tournaments
JavaScript
2
star
29

xsd

Prolog
2
star
30

CHR-Constraint-Store

Incremental constraint store for Prolog's Constraint Handling Rules (CHR) module.
Prolog
1
star
31

zeitpunkt

Tools to work with GeoJSON LineStrings with given time components
JavaScript
1
star
32

fnogatz.github.io

Personal Website of Falco Nogatz
JavaScript
1
star
33

cli-cal

[DEPRECATED] Github-like calendar graph for the command line
JavaScript
1
star
34

tconsole

Render objects in the console
JavaScript
1
star
35

swtparser.js

Parse SWT Swiss-Chess Tournament files with JavaScript
JavaScript
1
star
36

SWT-structure-files

Structure files for SWT Swiss Chess Tournament files
1
star
37

quickchess

OCR for chess game notations
TypeScript
1
star
38

uniwue-lernplaetze-scraper

Save the occupancy rates of working spaces in the libraries of the University of Würzburg, Germany.
JavaScript
1
star