> **_Go_** D-_Nix_ -- A Nix compiler into Delta Interaction Nets. Godnix is a compiler that translates Nix expressions using the [GoD-Net](https://github.com/vic/godnet) interaction net reduction engine. ## Features This first version supports: - **Integers**: Church numeral encoding - **Anonymous functions**: `x: body` syntax - **Function application**: Direct application of functions to arguments - **Let bindings**: `let x = value; in body` expressions - **Addition operator**: Pure Church numeral addition (`+`) ## Building ```bash go build -o godnix ./cmd/godnix ``` ## Testing Run the integration tests: ```bash go test ./cmd/godnix/ ``` For verbose output: ```bash go test -v ./cmd/godnix/ ``` The test suite includes: - Examples from `examples/` directory - Inline expression tests - Church numeral encoding/decoding - Error handling for unsupported features - Direct translator unit tests ## Usage ```bash ./godnix ``` ## Examples ### Simple Integer ```nix # examples/simple.nix 42 ``` Output: ``` 42 ``` ### Addition ```nix # examples/add.nix 2 + 3 ``` Output: ``` 5 ``` ### Identity Function ```nix # examples/identity.nix (x: x) 7 ``` Output: ``` 7 ``` ### Let Binding ```nix # examples/let.nix let x = 5; y = 3; in x + y ``` Output: ``` 8 ``` ### Lambda Functions ```nix # examples/lambda.nix let add = x: y: x + y; in add 4 6 ``` Output: ``` 10 ``` ## Architecture ### Components 1. **Parser** (`pkg/parser/`): Full-featured Nix parser (already present) 2. **Translator** (`pkg/compiler/translator.go`): Converts Nix AST to Lambda calculus terms 3. **Compiler** (`cmd/godnix/`): Main application that coordinates parsing, translation, and evaluation ### Translation Strategy Godnix translates Nix expressions to pure Lambda calculus: - **Integers** → Church numerals: `n = λf. λx. f^n x` - **Addition** → Church numeral addition: `λm. λn. λf. λx. m f (n f x)` - **Functions** → Lambda abstractions: `x: body` → `λx. body` - **Let bindings** → Function application: `let x = v in b` → `(λx. b) v` - **Application** → Standard application: `f x` → Lambda application ### Evaluation The compiled Lambda terms are evaluated using the godnet deltanet reduction engine, which implements optimal reduction through interaction nets. This provides: - Parallel reduction opportunities - Optimal sharing of computations - Efficient memory usage ## Performance The evaluator provides statistics after each run: - Execution time - Total reductions performed - Reductions per second Example output: ``` Stats: Time: 337.889µs Total Reductions: 60 (177573.11 ops/sec) ``` ## Limitations (v1) This first version has intentional limitations: - Only integers (no floats, strings, lists, sets) - Only `+` operator (no other arithmetic or comparison) - No built-in functions beyond `+` - No attribute sets or recursive bindings - No imports or file system operations These limitations allow focusing on the core compilation pipeline. The full Nix parser is available, so adding support for more features is straightforward. ## Future Directions Potential additions for future versions: - More arithmetic operators (`-`, `*`, `/`) - Boolean operations and conditionals - Lists and list operations - Attribute sets - Built-in functions (map, filter, fold) - String operations - Native functions for performance-critical operations ## License See LICENSE file.