Go D-Nix -- A Nix compiler into Delta Interaction Nets.
Godnix is a compiler that translates Nix expressions using the GoD-Net interaction net reduction engine.
This first version supports:
- Integers: Church numeral encoding
- Anonymous functions:
x: bodysyntax - Function application: Direct application of functions to arguments
- Let bindings:
let x = value; in bodyexpressions - Addition operator: Pure Church numeral addition (
+)
go build -o godnix ./cmd/godnixRun the integration tests:
go test ./cmd/godnix/For verbose output:
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
./godnix <file.nix># examples/simple.nix
42Output:
42
# examples/add.nix
2 + 3Output:
5
# examples/identity.nix
(x: x) 7Output:
7
# examples/let.nix
let
x = 5;
y = 3;
in
x + yOutput:
8
# examples/lambda.nix
let
add = x: y: x + y;
in
add 4 6Output:
10
- Parser (
pkg/parser/): Full-featured Nix parser (already present) - Translator (
pkg/compiler/translator.go): Converts Nix AST to Lambda calculus terms - Compiler (
cmd/godnix/): Main application that coordinates parsing, translation, and evaluation
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
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
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)
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.
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
See LICENSE file.