Skip to content

Commit 8fec199

Browse files
committed
Merge branch 'master' of https://github.com/engboris/stellogen into fix
2 parents 1e9b3f6 + 399fa0b commit 8fec199

File tree

14 files changed

+492
-63
lines changed

14 files changed

+492
-63
lines changed

CLAUDE.md

Lines changed: 259 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,259 @@
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.*

dune-project

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
(name stellogen)
2020
(synopsis "Stellogen is a minimalistic and logic-agnostic programming
2121
language based on term unification.")
22-
(depends base menhir (alcotest :with-test) sedlex ppx_deriving)
22+
(depends base menhir sedlex ppx_deriving)
2323
(tags
2424
("transcendental syntax" "logic programming" "constraint programming" "resolution logic" "unification" "self-assembly")))
2525

examples/prolog.sg

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,50 @@
1+
''' ARITHMETIC '''
2+
13
' unary addition
24
(:= add {
35
[(+add 0 Y Y)]
46
[(-add X Y Z) (+add (s X) Y (s Z))]})
57

68
' 2 + 2 = R
7-
(:= query [(-add <s s 0> <s s 0> R) R])
9+
(:= query1 [(-add <s s 0> <s s 0> R) R])
10+
(show (interact #add @#query1))
11+
12+
13+
''' FAMILY RELATIONSHIPS '''
14+
15+
' family facts
16+
(:= family {
17+
[(+parent tom bob)]
18+
[(+parent tom liz)]
19+
[(+parent bob ann)]
20+
[(+parent bob pat)]
21+
[(+parent pat jim)]})
22+
23+
' grandparent relationship
24+
(:= grandparent {
25+
[(-grandparent X Z) (-parent X Y) (+parent Y Z)]})
26+
27+
' who is tom's grandchild?
28+
(:= query5 [(-grandparent tom Z) Z])
29+
(show (interact #grandparent @(process #query5 #family)))
30+
831

9-
(show (interact #add @#query))
32+
''' GRAPH TRAVERSAL '''
1033

1134
(:= graph {
1235
[(+from 1) (+to 2)]
1336
[(+from 1) (+to 3)]
1437
[(+from 3) (+to 2)]
15-
'[(+from 4) (+to 3)]
16-
[(+from 3) (+to 4)]})
38+
[(+from 3) (+to 4)]
39+
[(+from 2) (+to 5)]})
1740

1841
(:= composition [(-to X) (-from X)])
1942

2043
' is there a path between 1 and 4?
21-
(:= query {
44+
(:= query6 {
2245
@[(-from 1)]
2346
[(-to 4) ok]})
2447

2548
<show interact (process
26-
#query
49+
#query6
2750
{ #graph #composition })>

nvim/ftdetect/stellogen.vim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
autocmd BufNewFile,BufRead *.sg setfiletype stellogen
2-
autocmd FileType stellogen setlocal shiftwidth=2 softtabstop=2 expandtab
2+
autocmd FileType stellogen setlocal shiftwidth=2 softtabstop=2 expandtab commentstring='\ %s

nvim/syntax/stellogen.vim

Lines changed: 43 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,53 @@
1-
syn clear
1+
if exists("b:current_syntax")
2+
finish
3+
endif
24

3-
syn keyword sgKeyword new declaration eval slice show use interact fire process
4-
syn match sgComment "\s*'[^'].*$"
5-
syn match sgId "#\%(\l\|\d\)\w*"
6-
syn region sgComment start="'''" end="'''" contains=NONE
5+
" Comments (must be early to take precedence)
6+
syn region sgCommentMulti start="'''" end="'''" contains=NONE
7+
syn match sgComment "'[^'].*$"
8+
9+
" Strings
710
syn region sgString start=/\v"/ skip=/\v\\./ end=/\v"/
8-
syn match sgSeparator "[\<\>\{\}\[\]|]"
9-
syn match sgOperator "@"
11+
12+
" Keywords
13+
syn keyword sgKeyword new declaration eval slice show use interact fire process spec
14+
syn keyword sgConstant ok
15+
16+
" Operators and separators
17+
syn match sgOperator ":="
1018
syn match sgOperator "::"
1119
syn match sgOperator "=="
12-
syn match sgOperator ":="
1320
syn match sgOperator "!="
21+
syn match sgOperator "||"
22+
syn match sgOperator "@"
23+
syn match sgSeparator "[\<\>\{\}\[\]|]"
24+
25+
" Polarity markers (+ or - before identifiers)
26+
syn match sgPolarity "[+-]\ze\w"
27+
28+
" Variables (uppercase starting identifiers)
29+
syn match sgVariable "\<[A-Z_]\w*\>"
30+
31+
" Defined identifiers in (:= X ...) - both simple and complex
32+
syn match sgDefinedId "\((:=\s\+\)\@<=[a-z_][a-z0-9_]*"
33+
syn match sgDefinedId "\((:=\s\+\)\@<=\d\+"
34+
syn match sgDefinedId "\((:=\s*(\)\@<=[^)]\+"
35+
36+
" Identifier references (prefixed with #)
37+
syn match sgIdRef "#[a-z_][a-z0-9_]*"
38+
syn match sgIdRef "#\d\+"
39+
syn match sgIdRef "#([^)]\+)"
1440

1541
hi link sgKeyword Keyword
16-
hi link sgId Identifier
42+
hi link sgConstant Constant
1743
hi link sgComment Comment
44+
hi link sgCommentMulti Comment
1845
hi link sgOperator Operator
19-
hi link sgSeparator Special
46+
hi link sgSeparator Delimiter
2047
hi link sgString String
48+
hi link sgPolarity Special
49+
hi link sgVariable Type
50+
hi link sgIdRef Identifier
51+
hi link sgDefinedId Function
52+
53+
let b:current_syntax = "stellogen"

0 commit comments

Comments
 (0)