How hazelgrove/hazel works
The Hazel project is a live functional-programming environment built around type theory and live evaluation of functional programs. It is implemented in ReasonML (a syntax layer over OCaml) and is compiled to JavaScript using js_of_ocaml for running in web browsers. The project is split into several key components that together form the complete interactive and evaluative environment.
Project Type and Purpose
• Hazel is a full-stack application that combines a powerful core language engine with a sophisticated web-based user interface.
• The core focus is on live functional programming, involving language parsing, type-checking, dynamic evaluation, pretty-printing, and real-time interactive updates.
• The README confirms that building and running Hazel involves compiling OCaml/ReasonML code (via dune and js_of_ocaml) and then serving the resulting JavaScript in web browsers.
Main Components
a. Core Engine (haz3lcore):
• This module encapsulates the heart of Hazel’s functionality. Inside haz3lcore you see further subdivisions:
• Language and Terms: Directories such as lang and lang/term handle language syntax, operators, patterns, and term representations.
• Dynamics and Statics: The dynamics subdirectory manages runtime evaluation (evaluator, environment, filter matching, etc.) while the statics directory deals with type-checking, elaboration, and binding resolution.
• Tiles and Projectors: These parts focus on the interactive UI representation of code; projectors convert internal language representations into visual components.
• Assistant and Clipboard features: Support for interactive help and managing user interactions.
b. Parser/Compiler Component (haz3lmenhir):
• This component uses Menhir (a parser generator for OCaml) to generate parsers. It processes the source code into an Abstract Syntax Tree (AST) and converts between different representations.
c. Web Interface (haz3lweb):
• This is the client-side front end that the user interacts with.
• It includes subdirectories for:
• Application logic (app) that handles the stateful interactions like editing, inspecting, and explaining code.
• Editors, decorations, and modes (e.g., cell editors, code editors, exercise settings) that manage the visual representation and user input.
• Debugging, exercises, and view components to support a live, interactive programming environment.
• Assets such as HTML, CSS, images, and JavaScript that implement the UI (found in www).
d. Other Supporting Libraries:
• There are additional modules such as pretty (for formatting and layout) and util (for helper functions, state management, and common utilities) which are used throughout all components.
e. Build and Testing Infrastructure:
• The top level contains build scripts (Makefile), dune project files, and test directories that illustrate how unit tests (with Alcotest) and continuous integration (with GitHub Actions and codecov) fit into the overall development lifecycle.
Relationships and Interactions
• The core engine (haz3lcore) provides language evaluation, type-checking, and transformation services that are utilized by both the parser (haz3lmenhir) and the web interface.
• haz3lmenhir processes source code input into an AST which is then fed to other components for elaboration and evaluation.
• The web interface (haz3lweb) serves as the presentation layer: it visualizes code, supports interactive editing, and displays evaluation results. It communicates with the core engine to reflect real-time changes and feedback.
• During build time, dune and Menhir coordinate to convert ReasonML/OCaml source code into JavaScript, linking the back-end processing with the final browser-based UI.
• Utility libraries “pretty” and “util” support formatting and helper operations across both front-end and core components.
Architectural Patterns and Principles
• Modularity: The project is organized in modules (core, menhir, web, pretty, util) with well-defined responsibilities.
• Separation of Concerns: Parsing is separated from evaluation and from UI—highlighting a clear separation between the language processing pipeline and presentation.
• Interactive Feedback: The design supports live feedback (debug mode, real-time evaluation) and dynamic UI updates.
• Build and Integration Pipeline: The use of dune for build orchestration, Makefile shortcuts, and js_of_ocaml for transpiling to JavaScript illustrates a multi-stage build process.