# hazelgrove/hazel architecture

> Architecture diagram of the GitHub repository hazelgrove/hazel, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-03-28). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/hazelgrove/hazel
- Repository: https://github.com/hazelgrove/hazel

## Overview

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.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Hazel Core Engine Subgraph
    subgraph "Hazel Core Engine (haz3lcore)"
        core1["haz3lcore: Core Engine"]:::core
        l["lang: Language"]:::core
        lt["lang/term: Terms"]:::core
        d["dynamics: Runtime Evaluation"]:::core
        s["statics: Type-checking"]:::core
        t["tiles: UI Tiles"]:::core
        p["projectors: UI Projectors"]:::core
        a["assistant: Interactive Help"]:::core
        cc["ClipboardCache"]:::core
    end

    %% Establish internal relationships for haz3lcore
    core1 --> l
    l --> lt
    core1 --> d
    core1 --> s
    core1 --> t
    core1 --> p
    core1 --> a
    core1 --> cc

    %% Hazel Parser/Compiler Subgraph
    subgraph "Hazel Parser/Compiler (haz3lmenhir)"
        menhir["haz3lmenhir: Parser/Compiler"]:::parser
        mlexer["Lexer.mll"]:::parser
        mparser["Parser.mly"]:::parser
        mast["AST.re"]:::parser
    end

    menhir --> mlexer
    menhir --> mparser
    menhir --> mast

    %% Hazel Web Interface Subgraph
    subgraph "Hazel Web Interface (haz3lweb)"
        web["haz3lweb: Web UI"]:::web
        main["Main.re: Application Logic"]:::web
        export["Export.re: State & Logic"]:::web
        editors["Editors (src/haz3lweb/app/editors)"]:::web
        debug["Debug (src/haz3lweb/debug)"]:::web
        exercises["Exercises (src/haz3lweb/exercises)"]:::web
        assets["Assets (www)"]:::web
    end

    web --> main
    web --> export
    web --> editors
    web --> debug
    web --> exercises
    web --> assets

    %% Supporting Libraries Subgraph
    subgraph "Supporting Libraries"
        pretty_node["pretty"]:::utility
        util_node["util"]:::utility
    end

    %% Build and Testing Infrastructure Subgraph
    subgraph "Build & Testing"
        build["Build Scripts (Makefile, dune, etc.)"]:::build
        tests["Test Suite"]:::build
    end

    %% External Interactions
    %% Parser output feeds into Core Engine
    mast -->|"AST"| core1

    %% Core Engine updates the Web Interface in real-time
    core1 -->|"RealTimeUpdates"| web

    %% Utility libraries support both Core Engine and Web Interface
    pretty_node --> core1
    pretty_node --> web
    util_node --> core1
    util_node --> web

    %% Build process compiles Parser, Core, and Web components
    build -->|"build"| menhir
    build -->|"build"| core1
    build -->|"build"| web
    tests -->|"CI/CD"| build

    %% Styling classes
    classDef core fill:#ADD8E6,stroke:#000,stroke-width:2px;
    classDef parser fill:#FFD580,stroke:#000,stroke-width:2px;
    classDef web fill:#90EE90,stroke:#000,stroke-width:2px;
    classDef utility fill:#DDA0DD,stroke:#000,stroke-width:2px;
    classDef build fill:#D3D3D3,stroke:#000,stroke-width:2px;

    %% Click Events for haz3lcore and its components
    click core1 "https://github.com/hazelgrove/hazel/tree/dev/src/haz3lcore"
    click l "https://github.com/hazelgrove/hazel/tree/dev/src/haz3lcore/lang"
    click lt "https://github.com/hazelgrove/hazel/tree/dev/src/haz3lcore/lang/term"
    click d "https://github.com/hazelgrove/hazel/tree/dev/src/haz3lcore/dynamics"
    click s "https://github.com/hazelgrove/hazel/tree/dev/src/haz3lcore/statics"
    click t "https://github.com/hazelgrove/hazel/tree/dev/src/haz3lcore/tiles"
    click p "https://github.com/hazelgrove/hazel/tree/dev/src/haz3lcore/projectors"
    click a "https://github.com/hazelgrove/hazel/tree/dev/src/haz3lcore/assistant"
    click cc "https://github.com/hazelgrove/hazel/blob/dev/src/haz3lcore/ClipboardCache.re"

    %% Click Events for haz3lmenhir and its components
    click menhir "https://github.com/hazelgrove/hazel/tree/dev/src/haz3lmenhir"
    click mlexer "https://github.com/hazelgrove/hazel/blob/dev/src/haz3lmenhir/Lexer.mll"
    click mparser "https://github.com/hazelgrove/hazel/blob/dev/src/haz3lmenhir/Parser.mly"
    click mast "https://github.com/hazelgrove/hazel/blob/dev/src/haz3lmenhir/AST.re"

    %% Click Events for haz3lweb and its components
    click web "https://github.com/hazelgrove/hazel/tree/dev/src/haz3lweb"
    click main "https://github.com/hazelgrove/hazel/blob/dev/src/haz3lweb/app/Main.re"
    click export "https://github.com/hazelgrove/hazel/blob/dev/src/haz3lweb/app/Export.re"
    click editors "https://github.com/hazelgrove/hazel/tree/dev/src/haz3lweb/app/editors"
    click debug "https://github.com/hazelgrove/hazel/tree/dev/src/haz3lweb/debug"
    click exercises "https://github.com/hazelgrove/hazel/tree/dev/src/haz3lweb/exercises"
    click assets "https://github.com/hazelgrove/hazel/tree/dev/src/haz3lweb/www"

    %% Click Events for Supporting Libraries
    click pretty_node "https://github.com/hazelgrove/hazel/tree/dev/src/pretty"
    click util_node "https://github.com/hazelgrove/hazel/tree/dev/src/util"

    %% Click Events for Build and Testing
    click build "https://github.com/hazelgrove/hazel/tree/dev/Makefile"
    click tests "https://github.com/hazelgrove/hazel/tree/dev/test"
```
