JSPM

  • Created
  • Published
  • Downloads 25
  • Score
    100M100P100Q77049F
  • License MIT

Compile and run Constraint Handling Rules (CHR) in JavaScript

Package Exports

  • chr

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (chr) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

CHR.js

Compile and run Constraint Handling Rules (CHR) in JavaScript

Install

npm install chr

Usage as CLI

If you install CHR.js via npm it will create a new chrjs executable:

$ chrjs /path/to/your.chr > /created/constrainthandler.js

If no path is specified, chrjs reads from stdin.

Usage with node.js

The CHR.js module can be used programmatically:

var chrjs = require('chr')

var code = chrjs.transform('a ==> b')
chrjs.transformFile('path.chr', function(err, code) {
  console.log(code)
})

Example

Consider the following CHR source code, which generates all fibonacci numbers upto a given index N as constraints of the form fib(Number,Value):

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

The CHR source code can be compiled to JavaScript by the use of the chrjs command:

$ echo 'upto(N), fib(A,AV), fib(B,BV) ==> B === A+1, B < N | fib(B+1,AV+BV);' | chrjs > fib.js

The generated JavaScript code has references to chr/runtime components, so make sure it is available. Open a REPL to play with the generated fib.js:

$ node
> var konsole = require('chr/console') // for graphical representation
> var CHR = require('./fib.js')        // load the generated source
> CHR.fib(1,1)                         // the first fibonacci is 0
> CHR.fib(2,1)                         // the second is 1
> konsole(CHR.Store)                   // print the content of the
┌────────────┐                         //   constraint store
│ Constraint │
├────────────┤
│ fib(1,1)   │
├────────────┤
│ fib(2,1)   │
└────────────┘
> CHR.upto(5)                          // generate the first 5 fibs
> konsole(CHR.Store)                   // print the content of the
┌────────────┐                         //   constraint store again
│ Constraint │
├────────────┤
│ fib(1,1)   │
├────────────┤
│ fib(2,1)   │
├────────────┤
│ upto(5)    │
├────────────┤
│ fib(3,2)   │
├────────────┤
│ fib(4,3)   │
├────────────┤
│ fib(5,5)   │
└────────────┘

More example CHR scripts are provided in the project's /examples directory.

Background

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

Todo

  • Provide an option to bundle the chr/runtime references to get a single-file package executable for example in browsers.
  • Add event emitters for rule application etc to provide an easy logging and debugging mechanism.