显示 HN:MOL – 一种管道可以自我追踪的编程语言
Show HN: MOL – A programming language where pipelines trace themselves

原始链接: https://github.com/crux-ecosystem/mol-lang

## MOL:一种用于AI/RAG管道的新语言 MOL是一种新颖的编程语言,专为构建AI和检索增强生成(RAG)管道而设计,解决了将现有AI工具连接在一起的“胶水代码”问题。与依赖Python或JavaScript的传统方法不同,MOL提供了原生管道操作符(`|>`)和**自动追踪**——自动计时和类型化管道中的每个步骤,以增强调试和可见性。 主要特性包括为AI量身定制的一等类型(如`Thought`、`Document`、`Embedding`),使用简洁的表达式简化RAG管道创建(例如`doc |> chunk(512) |> embed |> store("index")`),以及内置的安全防护,包括守卫断言和访问控制。 MOL可以编译为Python和JavaScript,确保可移植性。其核心优势在于管道操作符、自动追踪、AI特定类型和RAG重点功能的结合——这些功能在Python、Elixir或Rust等其他语言中不存在。 MOL可通过PyPI、Docker、Web Playground以及VS Code支持获得。它拥有不断增长的标准库(90多个函数)和全面的测试套件,使其成为简化AI开发的一个有前景的工具。

## MOL:一种自追踪管道语言 MouneshK 介绍了 MOL,一种新的领域特定编程语言,旨在构建和调试 AI 管道。MOL 的关键特性是其自动执行追踪——管道操作符 (`|>`) 本身会在每个管道步骤记录时间、类型和数据,无需手动日志记录或打印调试。 MOL 包含 12 种与 AI 相关的内置数据类型(如文档、嵌入和向量),并支持数据验证的保护断言。它可以转译为 Python 和 JavaScript,并拥有 90 多个函数的标准库。 该语言使用 Python 实现(约 3,500 行),并具有通过的测试套件,可通过 `pip install mol-lang` 安装。 还有一个基于 Web 的游乐场,可供立即实验。MOL 正在作为 CruxLabx 的认知计算平台 IntraMind 的一部分开发。 一位评论员指出,Elixir 提供了用于管道的调试工具,但没有像 MOL 那样内置的自动追踪功能。
相关文章

原文

MOL Language

PyPI version license python tests stdlib self-hosted docs intramind

The first programming language with native pipeline operators and auto-tracing — built for AI/RAG pipelines.

MOL Demo


Every AI pipeline today is glue code — Python scripts stitching together LangChain, LlamaIndex, vector DBs, and LLMs with no visibility into what happens between steps. MOL fixes this.

Problem How Python/JS Handle It How MOL Handles It
Pipeline Debugging Add print() everywhere, use logging libs Auto-tracing built into |> — every step timed & typed automatically
Data Flow Visibility No native pipe operator |> operator — data flows left-to-right, traced at every stage
Type Safety for AI Generic dicts, no domain types First-class types: Thought, Memory, Node, Document, Chunk, Embedding
RAG Boilerplate 50+ lines of setup code One expression: doc |> chunk(512) |> embed |> store("index")
Safety Rails Hope for the best guard assertions + access control at the language level
Portability Rewrite in each language Transpiles to Python and JavaScript from single .mol source

The Killer Feature: |> with Auto-Tracing

No other language has this combination:

Language Pipe Operator Auto-Tracing AI Domain Types RAG Built-in
Python No No No No
Elixir |> No No No
F# |> No No No
Rust No No No No
MOL |> Yes Yes Yes

Choose the method that works best for you:

1. Install from PyPI (Recommended)

Then use anywhere:

mol run hello.mol
mol repl
mol version
# Run a program
docker run --rm -v "$(pwd)":/app ghcr.io/crux-ecosystem/mol run /app/hello.mol

# Interactive REPL
docker run --rm -it ghcr.io/crux-ecosystem/mol repl

# Start the online playground
docker run --rm -p 8000:8000 ghcr.io/crux-ecosystem/mol playground

Image size: ~144 MB (Python 3.12-slim based)

git clone https://github.com/crux-ecosystem/mol-lang.git
cd mol-lang
python3 -m venv .venv
source .venv/bin/activate
pip install -e .

4. With LSP Support (for VS Code)

pip install mol-lang[lsp]

Then install the VS Code extension from mol-vscode/ or copy it:

cp -r mol-vscode/ ~/.vscode/extensions/mol-language-0.5.0

5. Online Playground (No Install)

Try MOL directly in your browser: http://135.235.138.217:8000


-- A full RAG ingestion pipeline in ONE expression
let doc be Document("notes.txt", "MOL is built for IntraMind pipelines.")

let index be doc |> chunk(30) |> embed |> store("my_index")

show index

Output:

  ┌─ Pipeline Trace ──────────────────────────────────────
  │ 0.  input                   ─  <Document:a3f2 "notes.txt" 39B>
  │ 1.  chunk(..)           0.1ms  → List<2 Chunks>
  │ 2.  embed               0.2ms  → List<2 Embeddings>
  │ 3.  store(..)           0.0ms  → <VectorStore:b7c1 "my_index" 2 vectors>
  └─ 3 steps · 0.3ms total ───────────────────────────
<VectorStore:b7c1 "my_index" 2 vectors>

Zero configuration. Every step timed, typed, and traced automatically.


-- Inferred type
let name be "IntraMind"
let count be 42

-- Explicit type annotation (mismatch = compile error)
let x : Number be 10
let msg : Text be "hello"
let flag : Bool be true

-- Reassignment
set count to count + 1
if score > 90 then
  show "excellent"
elif score > 70 then
  show "good"
else
  show "needs work"
end

while count < 10 do
  set count to count + 1
end

for item in range(5) do
  show to_text(item)
end
define greet(name)
  return "Hello, " + name + "!"
end

show greet("Mounesh")
-- This is a comment
show "code"  -- inline comment

The core of MOL. Data flows left → right through functions:

-- Single stage
"hello world" |> upper              -- "HELLO WORLD"

-- With arguments
"a,b,c" |> split(",")              -- ["a", "b", "c"]

-- Multi-stage chain (auto-traced when 3+ stages)
"  HELLO  " |> trim |> lower |> split(" ")

-- With custom functions
define double(x)
  return x * 2
end

5 |> double |> add_ten |> double    -- 40

Named, reusable pipelines:

pipeline preprocess(data)
  return data |> trim |> lower
end

let clean be "  RAW INPUT  " |> preprocess
show clean   -- "raw input"

Any pipe chain with 3+ stages automatically prints a trace:

  ┌─ Pipeline Trace ──────────────────────────────────────
  │ 0.  input                   ─  Text("  HELLO  ")
  │ 1.  trim                0.0ms  → Text("HELLO")
  │ 2.  lower               0.0ms  → Text("hello")
  │ 3.  split(..)           0.0ms  → List<1 strs>
  └─ 3 steps · 0.0ms total ───────────────────────────

Disable tracing with --no-trace:

mol run program.mol --no-trace

Type Purpose Constructor
Thought Cognitive unit with confidence score Thought("idea", 0.9)
Memory Persistent key-value with decay Memory("key", value)
Node Neural graph vertex with weight Node("label", 0.5)
Stream Real-time data buffer Stream("feed")
Type Purpose Constructor
Document Text document with source metadata Document("file.txt", "content...")
Chunk Text fragment from a document Chunk("text", 0, "source")
Embedding Vector embedding (64-dim, deterministic) Embedding("text", "model")
VectorStore In-memory vector index with similarity search Created via store()
trigger "event_name"           -- Fire an event
listen "event_name" do ... end -- Listen for events
link nodeA to nodeB            -- Connect nodes
process node with 0.3          -- Activate & adjust
evolve node                    -- Next generation
access "mind_core"             -- Request resource (checked!)
sync stream                    -- Synchronize data
emit "data"                    -- Emit to stream

Inline safety checks that halt execution on failure:

guard confidence > 0.8 : "Confidence too low for production"
guard len(data) > 0 : "Empty dataset"
guard answer |> assert_not_null

RAG Pipeline (Full Example)

-- 1. Create a document
let doc be Document("kb.txt", "Machine learning enables computers to learn. Deep learning uses neural networks.")

-- 2. Ingest: chunk → embed → store (ONE expression)
doc |> chunk(50) |> embed |> store("knowledge")

-- 3. Query
let results be retrieve("What is deep learning?", "knowledge", 3)

-- 4. Synthesize answer
let answer be results |> think("answer the question")

-- 5. Validate quality
guard answer.confidence > 0.5 : "Low confidence"

show answer.content

access "mind_core"      -- ✅ Allowed
access "memory_bank"    -- ✅ Allowed
access "secret_vault"   -- 🔒 DENIED — MOLSecurityError

Default allowed: mind_core, memory_bank, node_graph, data_stream, thought_pool.

let x : Number be "hello"   -- 🚫 MOLTypeError at declaration

Standard Library (90+ functions)

Category Functions
General len, type_of, to_text, to_number, range, abs, round, sqrt, max, min, sum, print
Functional map, filter, reduce, flatten, unique, zip, enumerate, count, find, find_index, take, drop, group_by, chunk_list, every, some
Math floor, ceil, log, sin, cos, tan, pi, e, pow, clamp, lerp
Statistics mean, median, stdev, variance, percentile
Collections sort, sort_by, sort_desc, binary_search, reverse, push, pop, keys, values, contains, join, slice
Strings split, upper, lower, trim, replace, starts_with, ends_with, pad_left, pad_right, repeat, char_at, index_of, format
Hashing & Encoding hash, uuid, base64_encode, base64_decode
Random random, random_int, shuffle, sample, choice
Map Utilities merge, pick, omit
Type Checks is_null, is_number, is_text, is_list, is_map
Serialization to_json, from_json, inspect
Time clock, wait
RAG Pipeline load_text, chunk, embed, store, retrieve, cosine_sim
Cognitive think, recall, classify, summarize
Debug display, tap, assert_min, assert_not_null

# Core
mol run <file.mol>                    # Run a program
mol run <file.mol> --no-trace         # Run without pipeline tracing
mol parse <file.mol>                  # Show AST tree
mol transpile <file.mol>              # Transpile to Python
mol transpile <file.mol> -t js        # Transpile to JavaScript
mol repl                              # Interactive REPL
mol version                           # Show version

# Package Manager (v0.5.0)
mol init                              # Initialize mol.json manifest
mol install <package>                 # Install a package
mol uninstall <package>               # Remove a package
mol list                              # List installed packages
mol search <query>                    # Search available packages
mol publish                           # Publish your package

# Browser/JS Compilation (v0.5.0)
mol build <file.mol>                  # Compile to standalone HTML (browser)
mol build <file.mol> --target js      # Compile to JavaScript
mol build <file.mol> --target node    # Compile to Node.js module
mol build <file.mol> -o output.html   # Custom output path

# LSP Server
mol lsp                               # Start language server (for editors)

mol transpile pipeline.mol --target python > output.py
mol transpile pipeline.mol --target js > output.js

Pipe chains are desugared into nested function calls:

-- MOL
"hello" |> upper |> split(" ")
# Python output
split(upper("hello"), " ")
// JavaScript output
split(upper("hello"), " ")

Full IDE support included in mol-vscode/:

  • LSP Server — Autocomplete (112 stdlib + keywords), hover docs, diagnostics, signature help, go-to-definition, document symbols
  • Syntax Highlighting — TextMate grammar
  • Auto-closing — Brackets and quotes
  • Code Foldingif...end, define...end, pipeline...end
  • 20+ Snippets — Quick templates
pip install mol-lang[lsp]
cp -r mol-vscode/ ~/.vscode/extensions/mol-language-0.5.0
# Restart VS Code

Packages & use Statement (v0.5.0)

MOL ships with 7 built-in packages:

Package Functions
std len, type_of, range, map, filter, reduce, sort, ...
math sqrt, pow, sin, cos, pi, e, floor, ceil, ...
text split, upper, lower, trim, replace, join, ...
collections flatten, unique, zip, group_by, sort_by, ...
crypto hash, uuid, base64_encode, base64_decode
random random, random_int, shuffle, sample, choice
rag chunk, embed, store, retrieve, cosine_sim, think
-- Import everything
use std

-- Import specific functions
use math : sqrt, pi

-- Alias
use text as T

let task be spawn do
  sleep(1000)
  "result"
end
show "main continues..."
let result be await task
let results be parallel(items, fn(x) -> process(x))
let ch be channel()
spawn do
  send(ch, "hello")
end
let msg be receive(ch)
let winner be race([task1, task2])    -- first to finish
let all be wait_all([task1, task2])   -- wait for all

let double be fn(x) -> x * 2
let result be [1, 2, 3] |> map(fn(x) -> x * x)
let grade be match score with
  | s when s >= 90 -> "A"
  | s when s >= 80 -> "B"
  | [x, y] -> f"pair({x}, {y})"
  | _ -> "default"
end
let timeout be config ?? 30         -- fallback if null
let msg be f"Hello {name}, you have {count} items"
let [first, ...rest] be [1, 2, 3, 4]
let {x, y} be {"x": 10, "y": 20}
try
  let data be risky_operation()
rescue e
  show f"Error: {e}"
ensure
  cleanup()
end
define greet(name, greeting be "Hello")
  show f"{greeting}, {name}!"
end
greet("World")          -- Hello, World!
greet("MOL", "Welcome") -- Welcome, MOL!
test "arithmetic" do
  assert_eq(2 + 2, 4)
  assert_true(10 > 5)
end

Run tests with: mol test (discovers all .mol files) or mol test myfile.mol.


Browser/JS Compilation (v0.5.0)

Compile MOL programs to standalone HTML or JavaScript:

mol build app.mol                  # → app.html (runs in browser)
mol build app.mol --target js      # → app.js (standalone JS)
mol build app.mol --target node    # → app.node.js (Node.js module)

Compiled output includes the complete MOL runtime (90+ stdlib functions ported to JavaScript). No dependencies required.


MOL/
├── mol/                        # Language implementation
│   ├── __init__.py             # Package metadata (v0.5.0)
│   ├── grammar.lark            # Lark EBNF grammar specification
│   ├── parser.py               # LALR parser + AST transformer
│   ├── ast_nodes.py            # 45+ AST node dataclasses
│   ├── interpreter.py          # Visitor-pattern interpreter with auto-tracing
│   ├── types.py                # Domain types (8 types)
│   ├── stdlib.py               # 90+ built-in functions
│   ├── transpiler.py           # Python & JavaScript transpiler
│   ├── lsp_server.py           # Language Server Protocol (LSP) server
│   ├── package_manager.py      # Package manager (init/install/publish)
│   ├── wasm_builder.py         # Browser/JS compilation
│   ├── runtime.js              # JavaScript runtime (90+ functions)
│   └── cli.py                  # CLI interface
├── docs/                       # MkDocs Material documentation source
├── examples/                   # 16 example programs
├── tutorial/                   # 6 tutorial files + cheatsheet
├── tests/test_mol.py           # 68 tests (all passing)
├── mol-vscode/                 # VS Code extension + LSP client
├── mkdocs.yml                  # MkDocs configuration
├── pyproject.toml              # Python project config
├── Dockerfile                  # Docker image (144 MB)
├── LANGUAGE_SPEC.md            # Formal language specification
├── CHANGELOG.md                # Version history
├── ROADMAP.md                  # Development roadmap
└── LICENSE                     # License

┌─────────────┐     ┌──────────────┐     ┌─────────────┐     ┌──────────────┐
│  .mol file  │ ──▶ │  Lark LALR   │ ──▶ │     AST     │ ──▶ │ Interpreter  │
│  (source)   │     │  Parser      │     │  (35+ node  │     │  (Visitor +  │
│             │     │              │     │   types)    │     │  Auto-Trace) │
└─────────────┘     └──────────────┘     └──────┬──────┘     └──────────────┘
                                                │
                                    ┌───────────┼───────────┐
                                    ▼           ▼           ▼
                              ┌──────────┐ ┌──────────┐ ┌──────────┐
                              │  Python  │ │   JS     │ │  (more)  │
                              │  Output  │ │  Output  │ │          │
                              └──────────┘ └──────────┘ └──────────┘

source .venv/bin/activate
python tests/test_mol.py

147 tests covering: variables, arithmetic, control flow, functions, recursion, lists, maps, strings, domain types, typed declarations, access control, events, pipes, guards, pipelines, chunking, embedding, vector search, full RAG integration, functional programming (map/filter/reduce), math functions, statistics, string algorithms, hashing, sorting, type checks, lambdas, pattern matching, null coalescing, string interpolation, destructuring, error handling, default parameters, built-in testing, spawn/await, channels, parallel map, race, concurrency patterns, field/index mutation, zero-arg lambdas, try/rescue with return, JSON functions, struct methods, and module system.


Version Highlights
v0.9.0 (current) Self-hosted codebase, web API server, IntraMind AI core, field/index mutation, serve(), json_parse/stringify, 147 tests
v0.8.0 Structs with methods, generators/iterators, file I/O, HTTP fetch, modules (use/export), 123 tests
v0.7.0 spawn/await, channels, parallel(), race(), wait_all(), sleep(), 102 tests
v0.6.0 Pattern matching, lambdas, ?? null safety, f"" interpolation, destructuring, try/rescue/ensure, default params, mol test
v0.5.0 Package manager, use statement, browser/JS compilation, JS runtime
v0.4.0 Docker support (144MB), LSP server, VS Code extension, 16 examples
v0.3.0 90+ stdlib functions, MkDocs docs, online playground
v0.2.0 RAG types (Document, Chunk, Embedding, VectorStore), full RAG pipeline
v0.1.0 Core language: pipes `

See CHANGELOG.md for full details.

See ROADMAP.md for the full plan.

Full documentation available at: https://crux-ecosystem.github.io/MOL/


Built for IntraMind by CruxLabx.

Creator: Mounesh Kodi

Proprietary — CruxLabx / IntraMind. All rights reserved.

联系我们 contact @ memedata.com