CHR.js Example Page

CHR.js is a just-in-time (JIT) compiler for Constraint Handling Rules (CHR), embedded in JavaScript. This is just a demo page of the browser usage. More examples, an interactive playground and the full documentation can be found at the project page at chrjs.net.

It is not recommended to use the CHR.js interpreter in production. Instead you should use babel-plugin-chr, an ahead-of-time (AOT) compiler for CHR.js, which ensures a better runtime performance.

Usage

For just-in-time (JIT) compilation in your browser simply include the chr.js in your page:
<script src="chr.js"></script>
If you want to do the parsing process separately (e.g. in a Webworker), you can use the smaller chr-wop.js:
<script src="chr-wop.js"></script>

Example

Open the JavaScript console of your browser: Ctrl + Shift + J.

  // create a new constraint handler
  var chr = new CHR()

  // add some rules
  chr('a(N) ==> N > 0 | a(N-1)')
  chr('a(0) <=>', function() { console.log('Removed a(0)') })

  // inspect the constraint store
  console.log(chr.Store.toString())

  // call a/1 constraint
  chr.a(10)

  // inspect updated constraint store
  console.log(chr.Store.toString())
      
More examples can be found at chrjs.net. This project page also provides a playground to interactively develop your constraint handler.

Screenshot

Screenshot of the executed code