CHR.Js: Compiling Constraint Handling Rules to Javascript Master Thesis at the University of Ulm

CHR.Js: Compiling Constraint Handling Rules to Javascript Master Thesis at the University of Ulm

Ulm University | 89069 Ulm | Germany Faculty of Engineering and Computer Science Institute of Software Engineering and Compiler Construction CHR.js: Compiling Constraint Handling Rules to JavaScript Master Thesis at the University of Ulm Submitted by: Falco Nogatz [email protected] Reviewer: Prof. Dr. Thom Frühwirth Prof. Dr. Enno Ohlebusch Consultant: Daniel Gall 2015 Version October 12, 2015 c 2015 Falco Nogatz This work is licensed under the Creative Commons. Attribution-NonCommercial-ShareAlike 3.0 License. To view a copy of this license, visit http://creativecommons.org/licenses/by-nc-sa/3.0/ or send a letter to Creative Commons, 543 Howard Street, 5th Floor, San Francisco, California, 94105, USA. Satz: PDF-LATEX 2ε Abstract Constraint Handling Rules (CHR) is a high-level programming language extension which introduces declarative multiset semantics. Although originally designed in the early 1990s, the number of implementations is still small. While there are adaptions for popular imperative programming languages such as C and Java, its most popular host language is Prolog. As a result, the dissemination of CHR is currently restricted almost entirely to research community. In this thesis we present an implementation of CHR in JavaScript. By embedding it into the dominating web programming language which recently got adopted for server-side frameworks as well, we open this declarative approach to a broad range of developers and new use cases. The embedding of CHR in JavaScript gives the chance to easily create applications with CHR in combination with front-end functions. As a result, we created a web-interface to explore the evaluation of Constraint Handling Rules interactively This visual CHR tracer is based on the created interpreter called CHR.js, whose main target is the full extensibility of CHR. To remain competitive with existing CHR implementations, we created a transpiler for precompilation of CHR.js source code. iii Contents 1. Introduction 1 1.1. Motivation . .2 1.2. Related Work . .3 1.2.1. Existing CHR systems . .3 1.2.2. Logic Programming in JavaScript . .3 1.3. Implementation Goals . .4 1.4. Scope of this Thesis . .5 1.5. Road Map . .6 2. Background: Constraint Handling Rules 7 2.1. Introduction . .8 2.2. Syntax . .8 2.2.1. Host Language Requirements . .8 2.2.2. Constraints . .9 2.2.3. Constraint Store . .9 2.2.4. Program and Rules . .9 Simplification Rule . 10 Propagation Rule . 10 Simpagation Rule . 11 Generalized Simpagation Form . 11 2.3. Semantics . 11 2.3.1. The Theoretical Operational Semantics ωt .............. 12 Execution State . 12 v Contents State Transition Rules . 13 2.3.2. The Refined Operational Semantics ωr ............... 14 2.3.3. Existing Extensions and Variations . 15 Rule Pragmas . 15 2.4. Example Program: gcd/1 ........................... 16 2.5. Compiling CHR . 17 2.5.1. Head Normal Form . 18 2.5.2. CHR in Logic Programming Languages . 19 2.5.3. CHR in Imperative Programming Languages . 22 Syntax of JCHR . 23 Syntax of CCHR . 25 Basic Compilation Scheme . 26 2.6. Summary . 28 3. Background: JavaScript 31 3.1. Introduction . 32 3.2. Targeted Runtime Environments . 33 3.3. The JavaScript Event Loop . 34 3.3.1. Synchronous and Asynchronous Functions . 37 3.3.2. Queueing Messages . 39 3.3.3. Stack Size . 40 3.3.4. Garbage Collection . 40 3.4. Example Program: gcd(a,b) ........................ 40 3.4.1. Iterative Implementation . 41 3.4.2. Recursive Implementation . 41 3.4.3. Asynchronous Implementation . 43 3.5. Syntax . 45 3.5.1. Prototype-based Inheritance . 45 3.5.2. Template Strings . 47 3.5.3. Tagged Template Strings . 48 3.5.4. Arrow Functions . 49 3.5.5. Desctructuring Assignments . 49 vi Contents 3.5.6. Promises . 50 3.6. Code Structuring . 54 3.7. Summary . 54 4. CHR.js – A CHR(JavaScript) Interpreter 57 4.1. Overview . 58 4.1.1. Integration . 59 4.2. Runtime Components . 59 4.2.1. Constraint: CHR.Constraint .................... 60 4.2.2. Store: CHR.Store ........................... 61 4.2.3. Propagation History: CHR.History ................. 63 4.2.4. Rule: CHR.Rule ............................ 63 4.2.5. CHR.js Program: CHR.Rules .................... 65 4.2.6. Helpers . 66 4.3. Parsing CHR.js Rules . 69 4.3.1. Example Program: gcd/1 ....................... 69 4.3.2. Syntax . 70 Built-in Constraints . 71 Guards . 72 Scope . 73 4.3.3. Rule Definition without Tagged Template Strings . 74 4.3.4. Parsing Expression Grammar . 75 Parser Generation . 78 4.4. Compilation . 79 4.4.1. Compilation Scheme for Occurrence Handlers . 80 4.4.2. Correctness . 83 4.4.3. Termination . 84 4.5. Advanced Usage . 84 4.5.1. Sequential Execution . 86 4.5.2. Parallel Execution . 87 4.6. Summary . 88 vii Contents 5. chrjs.net – Web-Based Tracing for CHR 91 5.1. The CHR Playground . 92 5.1.1. Screenshots . 93 5.1.2. Implementation Goals . 94 5.2. Architecture . 96 5.2.1. Adapted Grammar . 96 5.2.2. Parallelization Using Web Workers . 97 5.2.3. Limitations . 98 5.3. Interactive Tracing . 98 5.4. Future Work . 99 5.5. Summary . 99 6. Benchmarks and Optimizations 101 6.1. Benchmark Setup . 102 6.2. Comparison of gcd(a,b) Implementations in JavaScript . 103 6.3. Precompile CHR.js Source Code . 104 6.3.1. Babel – A JavaScript Transpiler . 104 6.3.2. Trampolines . 105 6.3.3. Basic Compilation Scheme using Trampolines . 107 6.4. Benchmark Result . 110 6.5. Summary . 111 7. Conclusion 113 7.1. Summary . 113 7.2. Conclusion . 114 7.3. Future Work . 115 A. Available Sources 117 B. Code Examples 119 B.1. Example Usage of Helper.forEach .................... 119 B.2. PEG.js Parsed Program gcd/1 ........................ 120 B.3. Generated Code for the Occurrence Handlers of gcd/1 .......... 125 viii Contents B.4. Precompiled Code for gcd/1 ......................... 129 C. User Manuals 137 C.1. CHR.js . 137 C.1.1. Installation . 137 node.js . 138 Browser Environments . 138 C.1.2. Usage Example with node.js . 139 C.1.3. Tests . 140 C.2. CHR.js-website . 141 C.3. CHR-Benchmarks . 141 C.3.1. Installation . 141 C.3.2..

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    163 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us