Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

chr

Package Overview
Dependencies
Maintainers
1
Versions
39
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

chr

Compile and run Constraint Handling Rules (CHR) in JavaScript

  • 0.0.1
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
84
increased by170.97%
Maintainers
1
Weekly downloads
 
Created
Source

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.

Keywords

FAQs

Package last updated on 24 Apr 2015

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc