# ellisk42/ec architecture

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

- Interactive diagram: https://gitdiagram.com/ellisk42/ec
- Repository: https://github.com/ellisk42/ec

## Overview

##### Project type and purpose
- DreamCoder is a research‐grade, multi‐language system (Python + OCaml + Rust) that implements a wake–sleep algorithm to learn small programs for a variety of “domains” (lists, text editing, Logo graphics, regex, etc.).
- It also embeds a few‐shot recognition network (Prototypical Networks) as an optional stage.

##### File structure
- Top‐level directories:
- bin/ — Python entry scripts for each domain (text.py, list.py, logo.py, tower.py, graphs.py, etc.)
- dreamcoder/ — core Python library: orchestration of wake–sleep phases, grammar, enumeration controller, likelihood model, checkpointing.
- solvers/ — OCaml code: domain primitives, the exhaustively enumerative solver, grammar/fragment routines.
- rust_compressor/ — Rust program: post‐enumeration compressor to identify shared subexpressions.
- protonet-networks/ — Python code (PyTorch) for few‐shot recognition.
- data/ & PBE_Strings_Track/ & data/sygus/ — task definitions, ground‐truth programs, benchmarks.
- docs/, tests/ — documentation and integration/unit tests.
- singularity — container recipe.
- Makefile, README, requirements.txt — build & dependency info.

##### README
- Confirms: Python scripts launch tasks, spawn OCaml solver processes, run recognition (neural net), call Rust compressor, checkpoint in pickles, graph results.
- Points to docs/software-architecture.md for deeper dive.

##### Main components & their interactions
a. Components:
1. Python Orchestrator (dreamcoder/ + bin/)
2. OCaml Solver Binaries (built from solvers/)
3. Rust Compressor (rust_compressor/)
4. Recognition Network (protonet-networks/)
5. Data Store (data/, PBE_Strings_Track/)
6. Checkpoint & Graphing (pickle files + bin/graphs.py)
7. Container/DevOps (singularity recipe, Makefile)

b. Relationships / Data flow:
- Python Orchestrator loads task definitions from Data Store.
- For each wake phase:
- Enumeration: orchestrator invokes OCaml Solver (via subprocess / shatter) → generates frontiers of candidate programs.
- Recognition: orchestrator calls Recognition Network to score and rank candidates (optional PyTorch).
- Compressor: orchestrator invokes Rust Compressor on frontiers → extracts shared fragments / updates DSL.
- Sleep phase: Python updates probabilistic grammar (fragmentGrammar.py) / likelihoodModel.py.
- Checkpoint: orchestrator writes Python pickle files with state.
- Graphing: bin/graphs.py reads pickle checkpoint → outputs performance plots.

c. Architectural patterns & principles:
- Pipeline (wake–sleep loop): sequential stages (enumerate → recognize → compress → update).
- Modular multi‐language: clear language boundaries (Python for orchestration, OCaml for enumeration, Rust for compression, PyTorch for recognition).
- Extensible domains: dreamcoder/domains subpackages for each problem domain; drop‐in new domains.
- Interprocess communication via subprocess calls and file I/O (JSON, pickles).

d. Key frameworks & libs:
- Python: ZMQ (maybe), dill/pickle, argparse, PyTorch (recognition), numpy.
- OCaml: ppx_jane, core, re2, yojson, menhir, ZeroMQ?
- Rust: serde_json, structopt, rayon?
- Container: Singularity.
- Tests: pytest / custom shell scripts.

##### Essential elements to capture
- Wake–sleep loop at center, with four major stages (enumeration, recognition, compression, grammar update).
- Multi‐language boundaries and subprocess invocation.
- Data inputs (task definitions) and outputs (pickles, graphs).
- Extensibility: domain plugin directory and prototypical networks folder.
- Build & container layer (Makefile, singularity) as an outer shell ensuring reproducibility.

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Container / DevOps
    subgraph "Container / DevOps"
        Makefile["Makefile"]:::devops
        Req["requirements.txt"]:::devops
        Singularity["singularity/"]:::devops
    end

    %% Data Store
    subgraph "Data Store"
        Data["Data Store\n(tasks, benchmarks)"]:::data
        PBE["PBE_Strings_Track/"]:::data
    end

    %% Python Orchestrator
    subgraph "Python Orchestrator"
        Launch["CLI Launch (bin/launch.py)"]:::python
        Core["Core Library\n(dreamcoder/)"]:::python
        Domains["Domain Plugins\n(dreamcoder/domains/)"]:::python
        Utilities["Utilities & Checkpoint I/O"]:::python
    end

    %% Wake–Sleep Loop
    subgraph "Wake–Sleep Loop"
        Enum["Enumeration\n(OCaml Solver)"]:::ocaml
        Recog["Recognition\n(PyTorch)"]:::pytorch
        Compress["Compression\n(Rust)"]:::rust
        Grammar["Grammar Update\n(fragmentGrammar.py)"]:::python
    end

    %% Graphing
    Graphing["Graphing Scripts\n(bin/graphs.py, taskRankGraphs.py,\ngraphVersionSizes.py)"]:::python

    %% Data Flow
    Data -->|loads tasks| Launch
    PBE -->|loads PBE tasks| Launch
    Launch -->|orchestrates| Core
    Core -->|calls via subprocess| Enum
    Enum -->|frontiers| Core
    Core -->|optional scoring| Recog
    Recog -->|scores candidates| Core
    Core -->|invokes| Compress
    Compress -->|fragments| Core
    Core -->|updates grammar| Grammar
    Grammar -->|updated grammar| Core
    Core -->|writes checkpoints| Utilities
    Utilities -->|checkpoint files| Graphing

    %% Container integration
    Makefile -->|builds & deps| Launch
    Req -->|deps| Launch
    Singularity -->|containerizes| Launch

    %% Click Events
    click Launch "https://github.com/ellisk42/ec/blob/master/bin/launch.py"
    click Core "https://github.com/ellisk42/ec/tree/master/dreamcoder/"
    click Domains "https://github.com/ellisk42/ec/tree/master/dreamcoder/domains/"
    click Utilities "https://github.com/ellisk42/ec/blob/master/dreamcoder/utilities.py"
    click Utilities "https://github.com/ellisk42/ec/blob/master/dreamcoder/taskBatcher.py"
    click Enum "https://github.com/ellisk42/ec/tree/master/solvers/"
    click Enum "https://github.com/ellisk42/ec/tree/master/solvers/Makefile"
    click Compress "https://github.com/ellisk42/ec/blob/master/rust_compressor/src/main.rs"
    click Compress "https://github.com/ellisk42/ec/blob/master/rust_compressor/Cargo.toml"
    click Compress "https://github.com/ellisk42/ec/tree/master/rust_compressor/Makefile"
    click Recog "https://github.com/ellisk42/ec/tree/master/prototypical-networks/protonets/"
    click Recog "https://github.com/ellisk42/ec/blob/master/prototypical-networks/protonet_server.py"
    click Recog "https://github.com/ellisk42/ec/blob/master/prototypical-networks/prototet_score.py"
    click Recog "https://github.com/ellisk42/ec/blob/master/prototypical-networks/ocaml_client.ml"
    click Recog "https://github.com/ellisk42/ec/blob/master/prototypical-networks/run_train.py"
    click Recog "https://github.com/ellisk42/ec/tree/master/prototypical-networks/scripts/"
    click Data "https://github.com/ellisk42/ec/tree/master/data/"
    click PBE "https://github.com/ellisk42/ec/tree/master/PBE_Strings_Track/"
    click Graphing "https://github.com/ellisk42/ec/blob/master/bin/graphs.py"
    click Graphing "https://github.com/ellisk42/ec/blob/master/bin/taskRankGraphs.py"
    click Graphing "https://github.com/ellisk42/ec/blob/master/bin/graphVersionSizes.py"
    click Makefile "https://github.com/ellisk42/ec/tree/master/Makefile"
    click Req "https://github.com/ellisk42/ec/blob/master/requirements.txt"
    click Singularity "https://github.com/ellisk42/ec/tree/master/singularity/"

    %% Styles
    classDef python fill:#ADD8E6,stroke:#333,stroke-width:1px
    classDef ocaml fill:#90EE90,stroke:#333,stroke-width:1px
    classDef rust fill:#FFA500,stroke:#333,stroke-width:1px
    classDef pytorch fill:#FFC0CB,stroke:#333,stroke-width:1px
    classDef data fill:#D3D3D3,stroke:#333,stroke-width:1px
    classDef devops fill:#D8BFD8,stroke:#333,stroke-width:1px
```
