# ekmett/thc architecture

> Architecture diagram of the GitHub repository ekmett/thc, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-10-07). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/ekmett/thc
- Repository: https://github.com/ekmett/thc

## Overview

THC runs Haskell programs on Truffle, using GHC as the frontend and Graal to specialize Core. The principal workflow is: a developer invokes the driver, Cabal builds the selected package, GHC Core is exported and acquired, the Core is audited and admitted, and THC executes the accepted entry point. Runtime capabilities include two execution backends, lazy values and typed data layouts, concurrency, native foreign calls, polyglot access, and runtime services. The excerpts show the Java driver and runtime entry points, but not Cabal acquisition internals; those links are documented in the README.

Main entry points: [`src/main/java/thc/Main.java`](https://github.com/ekmett/thc/blob/main/src/main/java/thc/Main.java) (THC driver), [`src/main/java/thc/Language.java`](https://github.com/ekmett/thc/blob/main/src/main/java/thc/Language.java) (Truffle language), [`src/main/java/thc/runtime/Program.java`](https://github.com/ekmett/thc/blob/main/src/main/java/thc/runtime/Program.java) (AST backend), [`src/main/java/thc/CoreModules.java`](https://github.com/ekmett/thc/blob/main/src/main/java/thc/CoreModules.java) (Core admission)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_frontend["Haskell frontend"]
  node_plugin["Core plugin<br/>[plugin.py]"]
end

subgraph group_acquisition["Core acquisition"]
  node_driver["THC driver<br/>[Main.java]"]
  node_json["JSON transport<br/>[Json.java]"]
  node_coreaudit["Core audit<br/>[audit-core.py]"]
  node_modules["Core admission<br/>[CoreModules.java]"]
end

subgraph group_execution["Truffle execution"]
  node_language["Truffle language<br/>[Language.java]"]
  node_program["AST backend<br/>[Program.java]"]
  node_bytecode["Bytecode backend"]
  node_executable["Program interface"]
  node_representation["Core representations"]
  node_representations["Representation inference"]
  node_data["Constructor values<br/>[DataLayout.java]"]
end

subgraph group_interop["Interop and services"]
  node_polyglot["Polyglot access<br/>[ForeignValue.java]"]
  node_foreign["Native interop"]
  node_services["Runtime services"]
end

node_developer(("Developer"))
node_cabal["Cabal build"]
node_ghc["GHC compiler"]

node_developer -->|"invokes"| node_driver
node_driver -->|"builds package"| node_cabal
node_cabal -->|"compiles Haskell"| node_ghc
node_ghc -->|"exports Core"| node_plugin
node_driver -->|"parses transport"| node_json
node_driver -->|"loads Core"| node_modules
node_plugin -.->|"produces audited input"| node_coreaudit
node_coreaudit -.->|"admits Core"| node_modules
node_driver -->|"launches context"| node_language
node_language -.->|"creates backend"| node_program
node_language -.->|"creates backend"| node_bytecode
node_program -.->|"implements"| node_executable
node_bytecode -.->|"implements"| node_executable
node_program -->|"uses proofs"| node_representation
node_program -->|"infers layouts"| node_representations
node_representation -.->|"guides storage"| node_data
node_language -.->|"exposes host access"| node_polyglot
node_modules -->|"admits imports"| node_foreign
node_language -.->|"owns runtime services"| node_services

click node_plugin "https://github.com/ekmett/thc/blob/main/bin/plugin.py"
click node_driver "https://github.com/ekmett/thc/blob/main/src/main/java/thc/Main.java"
click node_json "https://github.com/ekmett/thc/blob/main/src/main/java/thc/Json.java"
click node_coreaudit "https://github.com/ekmett/thc/blob/main/bin/audit-core.py"
click node_modules "https://github.com/ekmett/thc/blob/main/src/main/java/thc/CoreModules.java"
click node_language "https://github.com/ekmett/thc/blob/main/src/main/java/thc/Language.java"
click node_program "https://github.com/ekmett/thc/blob/main/src/main/java/thc/runtime/Program.java"
click node_bytecode "https://github.com/ekmett/thc/blob/main/src/main/java/thc/runtime/BytecodeProgram.java"
click node_executable "https://github.com/ekmett/thc/blob/main/src/main/java/thc/runtime/ExecutableProgram.java"
click node_representation "https://github.com/ekmett/thc/blob/main/src/main/java/thc/runtime/CoreRepresentation.java"
click node_representations "https://github.com/ekmett/thc/blob/main/src/main/java/thc/runtime/CoreRepresentations.java"
click node_data "https://github.com/ekmett/thc/blob/main/src/main/java/thc/runtime/DataLayout.java"
click node_polyglot "https://github.com/ekmett/thc/blob/main/src/main/java/thc/runtime/ForeignValue.java"
click node_foreign "https://github.com/ekmett/thc/blob/main/src/main/java/thc/ForeignBitcode.java"
click node_services "https://github.com/ekmett/thc/blob/main/src/main/java/thc/runtime/CoreRuntimeServices.java"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_plugin toneBlue
class node_driver,node_json,node_coreaudit,node_modules toneAmber
class node_language,node_program,node_bytecode,node_executable,node_representation,node_representations,node_data toneMint
class node_polyglot,node_foreign,node_services toneRose
class node_developer,node_cabal,node_ghc toneIndigo
```

## Components

### Haskell frontend

- [Core plugin](https://github.com/ekmett/thc/blob/main/bin/plugin.py): `bin/plugin.py`

### Core acquisition

- [THC driver](https://github.com/ekmett/thc/blob/main/src/main/java/thc/Main.java): `src/main/java/thc/Main.java`
- [JSON transport](https://github.com/ekmett/thc/blob/main/src/main/java/thc/Json.java): `src/main/java/thc/Json.java`
- [Core audit](https://github.com/ekmett/thc/blob/main/bin/audit-core.py): `bin/audit-core.py`
- [Core admission](https://github.com/ekmett/thc/blob/main/src/main/java/thc/CoreModules.java): `src/main/java/thc/CoreModules.java`

### Truffle execution

- [Truffle language](https://github.com/ekmett/thc/blob/main/src/main/java/thc/Language.java): `src/main/java/thc/Language.java`
- [AST backend](https://github.com/ekmett/thc/blob/main/src/main/java/thc/runtime/Program.java): `src/main/java/thc/runtime/Program.java`
- [Bytecode backend](https://github.com/ekmett/thc/blob/main/src/main/java/thc/runtime/BytecodeProgram.java): `src/main/java/thc/runtime/BytecodeProgram.java`
- [Program interface](https://github.com/ekmett/thc/blob/main/src/main/java/thc/runtime/ExecutableProgram.java): `src/main/java/thc/runtime/ExecutableProgram.java`
- [Core representations](https://github.com/ekmett/thc/blob/main/src/main/java/thc/runtime/CoreRepresentation.java): `src/main/java/thc/runtime/CoreRepresentation.java`
- [Representation inference](https://github.com/ekmett/thc/blob/main/src/main/java/thc/runtime/CoreRepresentations.java): `src/main/java/thc/runtime/CoreRepresentations.java`
- [Constructor values](https://github.com/ekmett/thc/blob/main/src/main/java/thc/runtime/DataLayout.java): `src/main/java/thc/runtime/DataLayout.java`

### Interop and services

- [Polyglot access](https://github.com/ekmett/thc/blob/main/src/main/java/thc/runtime/ForeignValue.java): `src/main/java/thc/runtime/ForeignValue.java`
- [Native interop](https://github.com/ekmett/thc/blob/main/src/main/java/thc/ForeignBitcode.java): `src/main/java/thc/ForeignBitcode.java`
- [Runtime services](https://github.com/ekmett/thc/blob/main/src/main/java/thc/runtime/CoreRuntimeServices.java): `src/main/java/thc/runtime/CoreRuntimeServices.java`

### Other components

- **Developer**
- **Cabal build**
- **GHC compiler**

## Connections

- Developer → THC driver: invokes (evidence: `README.md`)
- THC driver → Cabal build: builds package (evidence: `README.md`)
- Cabal build → GHC compiler: compiles Haskell (evidence: `README.md`)
- GHC compiler → Core plugin: exports Core (evidence: `README.md`)
- THC driver → JSON transport: parses transport (evidence: `src/main/java/thc/Main.java`)
- THC driver → Core admission: loads Core (evidence: `src/main/java/thc/Main.java`)
- Core plugin → Core audit: produces audited input
- Core audit → Core admission: admits Core
- THC driver → Truffle language: launches context (evidence: `src/main/java/thc/Main.java`)
- Truffle language → AST backend: creates backend (evidence: `src/main/java/thc/runtime/Program.java`)
- Truffle language → Bytecode backend: creates backend
- AST backend → Program interface: implements (evidence: `src/main/java/thc/runtime/Program.java`)
- Bytecode backend → Program interface: implements
- AST backend → Core representations: uses proofs (evidence: `src/main/java/thc/runtime/Program.java`)
- AST backend → Representation inference: infers layouts (evidence: `src/main/java/thc/runtime/Program.java`)
- Core representations → Constructor values: guides storage (evidence: `src/main/java/thc/runtime/DataLayout.java`)
- Truffle language → Polyglot access: exposes host access
- Core admission → Native interop: admits imports (evidence: `src/main/java/thc/CoreModules.java`)
- Truffle language → Runtime services: owns runtime services
