|  | 
|  | 1 | +# Stellogen - Project Guide for Claude Code | 
|  | 2 | + | 
|  | 3 | +## Overview | 
|  | 4 | + | 
|  | 5 | +Stellogen is an experimental, **logic-agnostic** programming language based on **term unification**. It explores a radically different approach to programming where both computation and meaning are built from the same raw material, without primitive types or fixed logical rules imposed from above. | 
|  | 6 | + | 
|  | 7 | +**Status:** Research project / proof of concept / esoteric language (not production-ready) | 
|  | 8 | + | 
|  | 9 | +## Core Philosophy | 
|  | 10 | + | 
|  | 11 | +Unlike traditional typed languages where types constrain and shape program design, Stellogen offers elementary interactive building blocks where computation and meaning coexist in the same language. The compiler/interpreter's role is reduced to checking that blocks connect - semantic power and responsibility belong entirely to the user. | 
|  | 12 | + | 
|  | 13 | +## Key Concepts | 
|  | 14 | + | 
|  | 15 | +### Term Unification | 
|  | 16 | +- The fundamental mechanism for both computation and typing | 
|  | 17 | +- Terms can be: | 
|  | 18 | +  - **Variables**: Uppercase start (e.g., `X`, `Y`, `Result`) | 
|  | 19 | +  - **Functions**: Lowercase or special symbol start (e.g., `(f X)`, `(add X Y)`) | 
|  | 20 | +- Unification finds substitutions that make two terms identical | 
|  | 21 | + | 
|  | 22 | +### Rays and Polarity | 
|  | 23 | +- Computation happens through **rays** with polarity: | 
|  | 24 | +  - `+` positive polarity | 
|  | 25 | +  - `-` negative polarity | 
|  | 26 | +  - neutral (no prefix) | 
|  | 27 | +- **Fusion**: Term interaction mechanism | 
|  | 28 | +- Example: `(+add 0 Y Y)` and `(-add X Y Z)` can fuse during interaction | 
|  | 29 | + | 
|  | 30 | +### Constellations | 
|  | 31 | +- Elementary computational blocks (analogous to logic clauses or functions) | 
|  | 32 | +- Defined using `{...}` with multiple rays/clauses | 
|  | 33 | +- Example: | 
|  | 34 | +  ``` | 
|  | 35 | +  (:= add { | 
|  | 36 | +    [(+add 0 Y Y)] | 
|  | 37 | +    [(-add X Y Z) (+add (s X) Y (s Z))]}) | 
|  | 38 | +  ``` | 
|  | 39 | + | 
|  | 40 | +### Stars and Interaction | 
|  | 41 | +- Stars are terms that can interact via `interact` | 
|  | 42 | +- `@` prefix: evaluates before interaction | 
|  | 43 | +- `#` prefix: reference to a defined term | 
|  | 44 | + | 
|  | 45 | +## Syntax Elements | 
|  | 46 | + | 
|  | 47 | +### Comments | 
|  | 48 | +- Single-line: `' comment text` | 
|  | 49 | +- Multi-line: `''' comment text '''` | 
|  | 50 | + | 
|  | 51 | +### Syntactic Sugar | 
|  | 52 | +- **Stack notation**: `<f a b c>` equivalent to `(f (a (b c)))` | 
|  | 53 | +- **Cons lists**: `[1|Tail]` for list construction | 
|  | 54 | +- **Groups**: `{...}` for constellations | 
|  | 55 | +- **Process chaining**: `(process X {Y Z})` chains constellations | 
|  | 56 | + | 
|  | 57 | +### Declarations | 
|  | 58 | +- **Definition**: `(:= name value)` | 
|  | 59 | +- **Macro**: `(new-declaration (pattern) (expansion))` | 
|  | 60 | +- **Show**: `(show expr)` - display result | 
|  | 61 | +- **Expect**: Assertion/testing mechanism | 
|  | 62 | + | 
|  | 63 | +### Syntax Reference | 
|  | 64 | +**See `examples/syntax.sg`** for comprehensive examples of all syntactic features including: | 
|  | 65 | +- Rays, stars, and constellations | 
|  | 66 | +- Focus (`@`) and identifiers (`#`) | 
|  | 67 | +- String literals, cons lists, and stack notation | 
|  | 68 | +- Linear (`fire`) vs non-linear (`interact`) execution | 
|  | 69 | +- Inequality constraints (`|| (!= X Y)`) | 
|  | 70 | +- Process chaining | 
|  | 71 | +- Fields and field access | 
|  | 72 | +- Nested structures | 
|  | 73 | +- File imports with `(use "path")` | 
|  | 74 | + | 
|  | 75 | +### Type System (Unconventional) | 
|  | 76 | +Types are defined as **sets of interactive tests**: | 
|  | 77 | +``` | 
|  | 78 | +(spec binary { | 
|  | 79 | +  [(-i []) ok]          ' returns [ok] on empty list | 
|  | 80 | +  [(-i [0|X]) (+i X)]   ' matches on [0] and checks the tail | 
|  | 81 | +  [(-i [1|X]) (+i X)]}) ' matches on [1] and checks the tail | 
|  | 82 | +``` | 
|  | 83 | + | 
|  | 84 | +Type checking: `(:: value type)` triggers interaction and expects `ok` | 
|  | 85 | + | 
|  | 86 | +## Project Structure | 
|  | 87 | + | 
|  | 88 | +``` | 
|  | 89 | +stellogen/ | 
|  | 90 | +├── src/              # OCaml source code | 
|  | 91 | +│   ├── sgen_ast.ml      # AST definitions | 
|  | 92 | +│   ├── sgen_eval.ml     # Evaluator | 
|  | 93 | +│   ├── sgen_parsing.ml  # Parser | 
|  | 94 | +│   ├── unification.ml   # Unification engine | 
|  | 95 | +│   ├── lexer.ml         # Lexer | 
|  | 96 | +│   ├── lsc_*.ml         # LSC (constellation) components | 
|  | 97 | +│   └── expr*.ml         # Expression handling | 
|  | 98 | +├── bin/              # Executable entry points | 
|  | 99 | +│   └── sgen.ml          # Main CLI | 
|  | 100 | +├── test/             # Test suite | 
|  | 101 | +├── examples/         # Example programs (.sg files) | 
|  | 102 | +│   ├── nat.sg           # Natural numbers | 
|  | 103 | +│   ├── prolog.sg        # Logic programming examples | 
|  | 104 | +│   ├── automata.sg      # Finite state machines | 
|  | 105 | +│   ├── lambda.sg        # Lambda calculus | 
|  | 106 | +│   └── ... | 
|  | 107 | +├── exercises/        # Learning exercises | 
|  | 108 | +└── nvim/             # Neovim integration | 
|  | 109 | +``` | 
|  | 110 | + | 
|  | 111 | +## Multi-Paradigm Support | 
|  | 112 | + | 
|  | 113 | +| Paradigm        | Stellogen Equivalent                                  | | 
|  | 114 | +|-----------------|-------------------------------------------------------| | 
|  | 115 | +| Logic           | Constellations (elementary blocks)                    | | 
|  | 116 | +| Functional      | Layered constellations enforcing interaction order    | | 
|  | 117 | +| Imperative      | Iterative recipes for building constellations         | | 
|  | 118 | +| Object-oriented | Structured constellations                             | | 
|  | 119 | + | 
|  | 120 | +## Building & Running | 
|  | 121 | + | 
|  | 122 | +### Build from Sources (Dune) | 
|  | 123 | +```bash | 
|  | 124 | +# Install dependencies | 
|  | 125 | +opam install . --deps-only --with-test | 
|  | 126 | + | 
|  | 127 | +# Build | 
|  | 128 | +dune build | 
|  | 129 | + | 
|  | 130 | +# Executables in: _build/default/bin/ | 
|  | 131 | +``` | 
|  | 132 | + | 
|  | 133 | +### Build with Nix | 
|  | 134 | +```bash | 
|  | 135 | +nix develop | 
|  | 136 | +dune build | 
|  | 137 | +``` | 
|  | 138 | + | 
|  | 139 | +### Running Programs | 
|  | 140 | +```bash | 
|  | 141 | +# Using built executable | 
|  | 142 | +./sgen.exe run <inputfile> | 
|  | 143 | + | 
|  | 144 | +# Using Dune | 
|  | 145 | +dune exec sgen run -- <inputfile> | 
|  | 146 | + | 
|  | 147 | +# Help | 
|  | 148 | +./sgen.exe --help | 
|  | 149 | +``` | 
|  | 150 | + | 
|  | 151 | +## File Extensions | 
|  | 152 | +- `.sg` - Stellogen source files | 
|  | 153 | +- `.mml` - Alternative extension (legacy?) | 
|  | 154 | + | 
|  | 155 | +## Example: Natural Number Addition | 
|  | 156 | + | 
|  | 157 | +```stellogen | 
|  | 158 | +' Define addition constellation | 
|  | 159 | +(:= add { | 
|  | 160 | +  [(+add 0 Y Y)] | 
|  | 161 | +  [(-add X Y Z) (+add (s X) Y (s Z))]}) | 
|  | 162 | +
 | 
|  | 163 | +' Query: 2 + 2 = R | 
|  | 164 | +(:= query [(-add <s s 0> <s s 0> R) R]) | 
|  | 165 | +
 | 
|  | 166 | +' Execute interaction | 
|  | 167 | +(show (interact #add @#query)) | 
|  | 168 | +``` | 
|  | 169 | + | 
|  | 170 | +## Example: Type Definition | 
|  | 171 | + | 
|  | 172 | +```stellogen | 
|  | 173 | +' Macro for type specification | 
|  | 174 | +(new-declaration (spec X Y) (:= X Y)) | 
|  | 175 | +
 | 
|  | 176 | +' Macro for type assertion | 
|  | 177 | +(new-declaration (:: Tested Test) | 
|  | 178 | +  (== @(interact @#Tested #Test) ok)) | 
|  | 179 | +
 | 
|  | 180 | +' Define nat type as interactive tests | 
|  | 181 | +(spec nat { | 
|  | 182 | +  [(-nat 0) ok] | 
|  | 183 | +  [(-nat (s N)) (+nat N)]}) | 
|  | 184 | +
 | 
|  | 185 | +' Define and check values | 
|  | 186 | +(:= 0 (+nat 0)) | 
|  | 187 | +(:: 0 nat)  ' succeeds | 
|  | 188 | +``` | 
|  | 189 | + | 
|  | 190 | +## Dependencies (OCaml) | 
|  | 191 | +- `base` - Standard library alternative | 
|  | 192 | +- `menhir` - Parser generator | 
|  | 193 | +- `sedlex` - Unicode-friendly lexer | 
|  | 194 | +- `ppx_deriving` - Code generation | 
|  | 195 | +- `alcotest` - Testing framework | 
|  | 196 | + | 
|  | 197 | +## Key Implementation Files | 
|  | 198 | + | 
|  | 199 | +- `src/sgen_ast.ml` - Core AST types: `sgen_expr`, `declaration`, `program` | 
|  | 200 | +- `src/unification.ml` - Term unification algorithm | 
|  | 201 | +- `src/sgen_eval.ml` - Expression evaluator and interaction engine | 
|  | 202 | +- `src/lsc_ast.ml` - Low-level constellation representation | 
|  | 203 | +- `bin/sgen.ml` - CLI entry point | 
|  | 204 | + | 
|  | 205 | +## Testing | 
|  | 206 | + | 
|  | 207 | +Run tests with: | 
|  | 208 | +```bash | 
|  | 209 | +dune test | 
|  | 210 | +``` | 
|  | 211 | + | 
|  | 212 | +## Influences & Related Work | 
|  | 213 | + | 
|  | 214 | +- **Prolog/Datalog**: Unification and logic programming | 
|  | 215 | +- **Smalltalk**: Minimalism and message-passing | 
|  | 216 | +- **Rocq/Coq**: Proof-as-program paradigm | 
|  | 217 | +- **Scheme/Racket**: Metaprogramming spirit | 
|  | 218 | +- **Shen**: Optional type systems philosophy | 
|  | 219 | +- **Girard's Transcendental Syntax**: Theoretical foundation | 
|  | 220 | + | 
|  | 221 | +## Learning Resources | 
|  | 222 | + | 
|  | 223 | +- **Wiki**: https://github.com/engboris/stellogen/wiki/Basics-of-Stellogen | 
|  | 224 | +- **Examples**: See `examples/` directory for practical demonstrations | 
|  | 225 | +- **README**: Project overview and philosophy | 
|  | 226 | + | 
|  | 227 | +## Working with this Codebase | 
|  | 228 | + | 
|  | 229 | +### When modifying: | 
|  | 230 | +1. The language is **experimental** - syntax and semantics change frequently | 
|  | 231 | +2. Understand term unification before touching `unification.ml` | 
|  | 232 | +3. AST changes require updates to parser, evaluator, and pretty-printer | 
|  | 233 | +4. Test with existing examples in `examples/` after changes | 
|  | 234 | + | 
|  | 235 | +### Important concepts for contributors: | 
|  | 236 | +- **Polarity** drives interaction - positive/negative rays fuse | 
|  | 237 | +- **Constellations** are the core computational unit | 
|  | 238 | +- **Stars** are terms prepared for interaction | 
|  | 239 | +- The evaluator orchestrates term interactions, not traditional evaluation | 
|  | 240 | + | 
|  | 241 | +### Debugging tips: | 
|  | 242 | +- Use `(show ...)` to inspect intermediate results | 
|  | 243 | +- Examine `examples/*.sg` for canonical usage patterns | 
|  | 244 | +- Parser errors: check parenthesis balance and syntax sugar | 
|  | 245 | +- Unification failures: verify term structure and polarity | 
|  | 246 | + | 
|  | 247 | +## License | 
|  | 248 | + | 
|  | 249 | +GPL-3.0-only | 
|  | 250 | + | 
|  | 251 | +## Maintainers | 
|  | 252 | + | 
|  | 253 | +- Author: Boris Eng | 
|  | 254 | +- Maintainer: Pablo Donato | 
|  | 255 | + | 
|  | 256 | +--- | 
|  | 257 | + | 
|  | 258 | +*Last updated: 2025-10* | 
|  | 259 | +*For current implementation details, always refer to the wiki and source code as the language evolves rapidly.* | 
0 commit comments