npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

chr

v3.3.21

Published

Interpreter for Constraint Handling Rules (CHR) in JavaScript

Downloads

54

Readme

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.